Sorting empty and word array in java - java

I want to write a code which can sort char array element. But the problem is where i want to sort 'a' before 'aa' element and I don't know how to write this part. It always sort 'aa' before 'a'. At first it get inputs from user and if we write '0' it will print sorted array.
import java.util.Scanner;
public class First {
public static void main(String[] args) {
int i, num = 0;
char[][] arr = new char[1000][1000];
char[] index = new char[1000];
Scanner myObj = new Scanner(System.in);
for(i = 0; i < 1000; i++){
arr[i] = myObj.next().toCharArray();
if(arr[i][0] == '0'){
break;
}
else{
num++;
}
for(int j = 0; j < i; j++){
if(arr[i][0] < arr[j][0]){
index = arr[i];
arr[i] = arr[j];
arr[j] = index;
j = 0;
}
}
}
for(i = 0; i < num; i++){
System.out.println(arr[i]);
}
}
}

You have to consider that 'aa' is not a char, instead 'a' is a char.
If you want to sort strings the code is nearly okay.
Here an example:
import java.util.Scanner;
public class First {
public static void main(String[] args) {
int num = 0;
String[] arr = new String[1000];
String index = "";
Scanner myObj = new Scanner(System.in);
for(int i = 0; i < 1000; i++){
arr[i] = myObj.nextLine();
if(arr[i].equals("0")){
break;
}
else{
num++;
}
for(int j = 0; j < i; j++){
if(arr[i].compareTo(arr[j]) < 0){
index = arr[i];
arr[i] = arr[j];
arr[j] = index;
j = 0;
}
}
}
System.out.print("[ ");
for(int i = 0; i < num; i++){
System.out.print(arr[i] + " ");
}
System.out.println("]");
}
}
Input:
aaaaaaaa
aaaaaaa
aaaaaa
aaaaa
aaaa
aaa
aa
a
0
Expected Output:
[ a aa aaa aaaa aaaaa aaaaaa aaaaaaa aaaaaaaa ]

It is only sorting by the first char of an entered word.
Thus it seems like the input-order is preserved.
Issue
Adding some debug-prints shows the comparison of first-char is leading to incorrect or unwanted aa before a case:
import java.util.Scanner;
public class First {
public static void main(String[] args) {
int i, num = 0;
char[][] arr = new char[1000][1000];
char[] index = new char[1000];
Scanner myObj = new Scanner(System.in);
for(i = 0; i < 1000; i++){
arr[i] = myObj.next().toCharArray();
if(arr[i][0] == '0'){
break;
}
else{
num++;
}
System.out.printf("Debug [%d]: '%s' \n", i, String.valueOf(arr[i]));
for(int j = 0; j < i; j++){
System.out.printf("Compare '%s' < '%s' = %s\n", arr[i][0], arr[j][0], (arr[i][0] < arr[j][0]));
if(arr[i][0] < arr[j][0]){
index = arr[i];
arr[i] = arr[j];
arr[j] = index;
j = 0;
}
}
}
for(i = 0; i < num; i++){
System.out.println(arr[i]);
}
}
}
Output:
a
Debug [0]: 'a'
aa
Debug [1]: 'aa'
Compare 'a' < 'a' = false
0
a
aa
Refactored and solved
I just added a bit output to interact with user.
Also refactored a bit (extract into methods, and control length of arrays with a configurable constant).
import java.util.Scanner;
public class First {
private static final int LENGTH = 10;
private static final Scanner myObj = new Scanner(System.in);
public static void main(String[] args) {
char[][] arr = new char[LENGTH][LENGTH];
System.out.println("Enter elements (each on a new line, 0 stops):");
int num = readArray(0, arr);
System.out.printf("Printing %d elements:\n", num);
printArray(num, arr);
}
private static int readArray(int num, char[][] arr) {
char[] index;
int i;
for (i = 0; i < LENGTH; i++) {
arr[i] = readChars();
if (arr[i][0] == '0') {
break;
} else {
num++;
}
for (int j = 0; j < i; j++) {
if (String.valueOf(arr[i]).compareTo(String.valueOf(arr[j])) < 0) { // entire array compared (chars in sequence) instead only: arr[i][0] < arr[j][0]
index = arr[i];
arr[i] = arr[j];
arr[j] = index;
j = 0;
}
}
}
return num;
}
private static void printArray(int num, char[][] arr) {
int i;
for (i = 0; i < num; i++) {
System.out.println(arr[i]);
}
}
private static char[] readChars() {
return myObj.next().toCharArray();
}
}
Output is as expected (a before aa):
Enter elements (each on a new line, 0 stops):
z
aa
b
a
0
Printing 4 elements:
a
aa
b
z
How it works
Entire array compared (chars in sequence) instead only the first char of each array.
before:
arr[i][0] < arr[j][0]
after:
String.valueOf(arr[i]).compareTo(String.valueOf(arr[j])) < 0
Bonus Tip: Naming can help to spot logical bugs
When renaming the methods and variable names it may get a bit clearer what the program does, we call it semantics:
myObj becomes scanner
i becomes index or wordIndex or lineIndex and lastLineIndex
j is actually a character-index ... but in this short scope it should can be self-evident
char-arrays can be lines or words
num becomes length of lines or countLines
and all the method-names are adjusted in semantics to operate on lines, expressed by name <verb>Lines
import java.util.Scanner;
public class First {
private static final int LENGTH = 10;
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
char[][] lines = new char[LENGTH][LENGTH];
System.out.println("Enter elements (each on a new line, 0 stops):");
int countLines = readLines(lines);
System.out.printf("Printing %d elements:\n", countLines);
printLines(lines, countLines);
}
private static int readLines(char[][] lines) {
int linesRead = 0;
for (int lineIndex = 0; lineIndex < LENGTH; lineIndex++) {
lines[lineIndex] = readLine();
if (lines[lineIndex][0] == '0') {
break;
} else {
linesRead++;
}
sortLines(lines, lineIndex);
}
return linesRead;
}
private static void sortLines(char[][] lines, int lastLineIndex) {
for (int j = 0; j < lastLineIndex; j++) {
if (String.valueOf(lines[lastLineIndex]).compareTo(String.valueOf(lines[j])) < 0) { // entire array compared (chars in sequence) instead only: arr[i][0] < arr[j][0]
char[] line = lines[lastLineIndex];
lines[lastLineIndex] = lines[j];
lines[j] = line;
j = 0;
}
}
}
private static void printLines(char[][] lines, int length) {
for (int i = 0; i < length; i++) {
System.out.println(lines[i]);
}
}
private static char[] readLine() {
return scanner.next().toCharArray();
}
}

Related

Create an algorithm that arranges them in order to form the largest possible integer

I am struggling with this coding problem in Java. It has to be solved using an algorithm. It is supposed to give me the largest possible inter that can be formed with the numbers in the array: {10, 7, 76, 415}. I tried to solve it using selection sort. So far I have is this code below but it's giving me the wrong output. My code:
public class largestNumber {
public static int largestNumber(int[] arr) {
for (int i = 1; i < arr.length -1; i++) {
for (int j = i +1; j < arr.length; j++) {
String temp1 = Integer.toString(i) + Integer.toString(j);
String temp2 = Integer.toString(j) + Integer.toString(i);
if (Integer.parseInt(temp2) > Integer.parseInt(temp1)) {
int swap = arr[i];
arr[i] = arr[j];
arr[j] = swap;
}
}
}
String ansNum = "";
for (int i = 0; i < arr.length; i++) {
ansNum += Integer.toString(arr[i]);
}
return Integer.parseInt(ansNum);
}
public static void main(String[] args) {
int [] arr = {10, 7, 76, 415};
int output = largestNumber(arr);
System.out.println(output); //Print out the number
}
}
The given output from this code is:10415767
But the right answer is: 77641510
Please give your feedback and thoughts.
Fix the bugs as shown below and it should work.
for (int i = 0; i < arr.length; i++) { //correction here
for (int j = i +1; j < arr.length; j++) {
String temp1 = Integer.toString(arr[i]) + Integer.toString(arr[j]); //correction here
String temp2 = Integer.toString(arr[j]) + Integer.toString(arr[i]); //correction here
To write some elegant code, I would have used a comparator as shown below:
String[] stringArr = new String[arr.length];
for(int i = 0; i < arr.length; i++)
stringArr[i] = String.valueOf(arr[i]);
Comparator<String> comp = new Comparator<String>(){
#Override
public int compare(String str1, String str2){
String s1 = str1 + str2;
String s2 = str2 + str1;
return s2.compareTo(s1);
}
};
Arrays.sort(stringArr, comp);
String ansNum = "";
for(int i = 0; i < stringArr.length; i++) {
ansNum += stringArr[i];
}
return Integer.parseInt(ansNum);
To talk about time complexity, the two for loops in your code makes this solution O(n^2) whereas with the use of sort it becomes O(nlog(n)) where n is the number of elements in the input array. So, not only the code becomes elegant with the use of comparator, in this case it becomes efficient too.
public class largestNumber {
public static int largestNumber(int[] arr) {
for (int i = 0; i < arr.length -1; i++) {
for (int j = i +1; j < arr.length; j++) {
String temp1 = Integer.toString(arr[i]) + Integer.toString(arr[j]);
String temp2 = Integer.toString(arr[j]) + Integer.toString(arr[i]);//rectified
if (Integer.parseInt(temp2) > Integer.parseInt(temp1)) {
int swap = arr[i];
arr[i] = arr[j];
arr[j] = swap;
}
}
}
String ansNum = "";
for (int i = 0; i < arr.length; i++) {
ansNum += Integer.toString(arr[i]);
}
return Integer.parseInt(ansNum);
}
public static void main(String[] args) {
int [] arr = {10, 7, 76, 415};
int output = largestNumber(arr);
System.out.println(output); //Print out the number
}
}

How can I sort using two different packages?

I had an assignment for a class where I had to develop a simple number sort program.
My main is supposed to receive the user input and my sort class is supposed to interrupt and spit out the resulting numbers in ascending and descending order. The problem is that my main is taking the input but it's not putting it in order at all and I'm unsure why.
package main;
import sort.Sort;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int arr[] = new int[5];
Scanner myScanner = new Scanner(System.in);
for(int i=0; i<5; i++) {
System.out.print("Enter a number: ");
myScanner.nextLine();
}
Sort sortObj = new Sort();
sortObj.ascendingsort(arr);
}
}
package sort;
public class Sort {
public void ascendingsort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
void descendingsort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
}
I know I'm missing something in my Main, because I'm fairly certain I don't need to put anything else into the sort class.
You're code is perfect. You just missed one assignment in your main method.
Instead of myScanner.nextLine(); you should have written arr[i] = myScanner.nextLine();
myScanner.nextLine(); is getting the next line you enter in the console but it isn't saving it anywhere. arr[i] = myScanner.nextLine(); will save the value of the console in the arr array.
Everything else should work after that.
First you are getting user input using myScanner.nextLine() but not storing it. myScanner.nextLine() won't save anything which user enters .
Thus you need to store it in an array and then use it later. So your main method after the changes should be like this.
public static void main(String[] args) {
int arr[] = new int[3];
Scanner myScanner = new Scanner(System.in);
for(int i=0; i<3; i++) {
System.out.print("Enter a number: ");
arr[i] = myScanner.nextInt();
}
Sort sortObj = new Sort();
sortObj.ascendingsort(arr);
}
And your sorting functions are also wrong. There you do some mis-calculations and return / log nothing. Thus you will not see anything in the console if you are not logging or returning anything.
public void descendingsort(int array[]) {
int n = array.length;
int i, j, temp;
for (i = 0; i < ( n- 1 ); i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] < array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for (i = 0; i < n; i++){
System.out.println(array[i]);
}
}
public void ascendingsort(int array[]) {
int n = array.length;
int i, j, temp;
for (i = 0; i < ( n- 1 ); i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for (i = 0; i < n; i++){
System.out.println(array[i]);
}
}
I hope this functions will work for your use case of ascending and descending sort.

line 21 show ; expected

The first line contains a single integer p denoting the length of array. The second line contains space-separated integers describing each respective element in array. The third line prints an integer indicating the number of negative arrays.
package asgn3;
import java.util.*;
public class Asgn3 {
public static void main(String[] args) {
int count = 0, result = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter the array ");
String s = in.nextLine();
int j = 0;
String[] s1 = s.split(" ");
int a[] = new int[s1.length];
for(String s2:s1) {
a[j] = Integer.parseInt(s2));
j++;
}
for (int i = 0; i < a.length; i++) {
for ( j = i; j < a.length; j ++) {
for (int k = i; k <= j; k++) {
result += a[k];
}
if(result < 0)
count ++;
}
System.out.println("no. of negatve arrays is "+count);
}
}
}
The issues is usage of extra unnecessary parenthesis. Change,
a[j]=Integer.parseInt(s2));
with
a[j]=Integer.parseInt(s2);
Remove extra bracket ) from end of line
a[j] = Integer.parseInt(s2);

PROGRAM to find least common entry in array

I was writing the code for the least occurring element in the array and for some reason my logic goes wrong and the compiler just prints either the first or the second element in the array? anyone know what's wrong?
package javaapplication10;
import java.util.*;
public class JavaApplication10 {
public static void main(String[] args) {
int m =1000;
int count = 0;
int store = 0;
int c = 0;
Scanner scan = new Scanner(System.in);
int[] a = new int[20] ;
int n;
System.out.print("Enter no of elements");
n = scan.nextInt();
for(int i =0; i<n;i++) {
a[i] = scan.nextInt();
}
for(int i =0; i <n ; i++) {
c = a[i] ;
for(int j =0; j <n ; j++) {
if(a[j] ==c) {
count++ ;
}
if(j == (n-1)) {
if(count<m ) {
store = a[i];
m = count;
}
}
count = 0;
}
}
System.out.print(store);
}
}
A better solution is to do sorting. We first sort the array, then linearly traverse the array.
static int leastFrequent(int arr[], int n)
// n is length of array
{
// Sort the array
Arrays.sort(arr);
// find the min frequency using
// linear traversal
int min_count = n+1, res = -1;
int curr_count = 1;
for (int i = 1; i < n; i++) {
if (arr[i] == arr[i - 1])
curr_count++;
else {
if (curr_count < min_count) {
min_count = curr_count;
res = arr[i - 1];
}
curr_count = 1;
}
}
// If last element is least frequent
if (curr_count < min_count)
{
min_count = curr_count;
res = arr[n - 1];
}
return res;
}
I guess you are trying to implement the following logic
Find the count of each element in the array
If you find an element with lower count - store the element
Repeat for each element in the array - to find the lowest element.
You should have rest the count at the end of the inner loop as,
package javaapplication10;
import java.util.*;
public class JavaApplication10 {
public static void main(String[] args) {
int m =1000;
int count = 0;
int store = 0;
int c = 0;
Scanner scan = new Scanner(System.in);
int[] a = new int[20] ;
int n;
System.out.print("Enter no of elements");
n = scan.nextInt();
for(int i =0; i<n;i++) {
a[i] = scan.nextInt();
}
for(int i =0; i <n ; i++) {
c = a[i] ;
for(int j =0; j <n ; j++) {
if(a[j] ==c) {
count++ ;
}
if(j == (n-1)) {
if(count<m ) {
store = a[i];
m = count;
}
}
}
count = 0;
}
System.out.print(store);
}
}
You have a single counter, so you'll lose this counting once you transition from one element to another.
You could hold an auxiliary map of counters and update it as you go, but frankly, using Java's streams will save you a lot of boilerplate code:
int leastOccuring =
Arrays.stream(a)
.boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())
.entrySet()
.stream()
.min(Map.Entry.comparingByValue())
.map(Map.Entry::geyKey)
.get();
This would be the correct code for your program.
import java.util.*;
public class LeastOccuringElementInArray {
public static void main(String[] args) {
int m = 0;
int count = 0;
int store = 0;
int c = 0;
Scanner scan = new Scanner(System.in);
int[] a = new int[20] ;
int n;
System.out.print("Enter no of elements");
n = scan.nextInt();
for(int i =0; i<n;i++)
{
a[i] = scan.nextInt();
}
for(int i =0; i <n ; i++)
{ c = a[i] ;
for(int j =0; j <n ; j++)
{
if(a[j] == c)
{
count++ ;
}
if(j == (n-1))
{
if(m!=0 && m > count)
{
store = a[i];
m = count;
}
else {
m = count;
}
}
}
count = 0;
}
System.out.print(store);
scan.close();
}
}

Sorting BigDecimal

What is the problem with this BigDecimalSorting? The code takes in numbers as string and then converts it to BigDecimal and then sort and print the sorted BigDecimals.
import java.math.BigDecimal;
import java.util.*;
class Solution {
public static void main(String []argh){
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
String []s=new String[n];
BigDecimal a[] = null;
for(int i = 0; i < n ; i++){
s[i]=sc.next();
a[i] = new BigDecimal(s[i]);
}
for(int i = 0; i < n-1; i++){
for(int j = 1; j < n; j++){
if(a[i].compareTo(a[j]) == -1){
BigDecimal temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
//Output
for(int i=0;i<n;i++){
s[i] = a[i].toString();
System.out.println(s[i]);
}
}
}
Sample Input:
9
-100
50
0
56.6
90
0.12
.12
02.34
000.000
Expected Output:
90
56.6
50
02.34
0.12
.12
0
000.000
-100
You can use the comparator to sort the BigDecimal
import java.math.BigDecimal;
import java.util.*;
class Solution{
public static void main(String []args){
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
String []s=new String[n+2];
for(int i=0;i<n;i++){
s[i]=sc.next();
}
sc.close();
Arrays.sort(s, 0, n, new Comparator<Object>() {
public int compare(Object a1, Object a2) {
BigDecimal bigDec1 = new BigDecimal((String) a1);
BigDecimal bigDec2 = new BigDecimal((String) a2);
return bigDec2.compareTo(bigDec1);
}
});
//Output
for(int i=0;i<n;i++)
{
System.out.println(s[i]);
}
}
import java.math.BigDecimal;
import java.util.*;
class Solution{
public static void main(String []argh)
{
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
String []s=new String[n+2];
for(int i=0;i<n;i++)
{
s[i]=sc.next();
}
for(int i=0;i<n;i++)
{
BigDecimal max=new BigDecimal(s[i]);
int idx=i;
for(int j=i+1;j<n;j++)
{
BigDecimal curr=new BigDecimal(s[j]);
if(curr.compareTo(max)==1)
{
max=curr;
idx=j;
}
}
String temp=s[i];
s[i]=s[idx];
s[idx]=temp;
}
for(int i=0;i<n;i++)
{
System.out.println(s[i]);
}
}
}
Looks like you're throwing an NPE because you're trying to access a null array.
BigDecimal a[] = null; // <---- null array
for (int i = 0; i < n; i++) {
s[i] = sc.next();
a[i] = new BigDecimal(s[i]); // <---- accessing null array a
}
Try initializing your array with the n length used on input
BigDecimal a[] = new BigDecimal[n];
Edit
in response to Mariano's answer, your
if (a[i].compareTo(a[j]) == -1)
is correct, as is. See BigDecimal javadoc
Returns:
-1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val.
You have a couple of errors in your code.
First you are not initializing the a array, BigDecimal a[] = null should be replaced by BigDecimal a[] = new BigDecimal[n] to avoid the NullPointerException.
Finally you are implementing the sort algorithm wrong, you should replace the inner for (int j = 1; j < n; j++) with for (int j = i + 1; j < n; j++)
Here is how your code should look like:
import java.math.BigDecimal;
import java.util.Scanner;
public class Solution {
public static void main(final String[] argh) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] s = new String[n];
BigDecimal a[] = new BigDecimal[n];
for (int i = 0; i < n; i++) {
s[i] = sc.next();
a[i] = new BigDecimal(s[i]);
}
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i].compareTo(a[j]) == -1) {
BigDecimal temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
// Output
for (int i = 0; i < n; i++) {
s[i] = a[i].toString();
System.out.println(s[i]);
}
}
}

Categories