0

What the code does is, basically, asks for number of elements that you want to sort,the elements and prints them sorted. Or at least it should. When I run it it would do that:

enter image description here

What should I do? I'm using JDK 7 and IntelliJ IDEA 15.

And yes, I did google it and I couldn't find anything. And no , I don't want the code, I want opinions.

import java.text.MessageFormat;
import java.util.Scanner;

public class bb {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter number of elements:");
        int numberOfElements = scan.nextInt();
        System.out.print(MessageFormat.format("Enter {0} {1} ",numberOfElements,"numbers: "));
        int [] elements = new int[numberOfElements];
        for (int i = 0; i < elements.length; i++) {
            elements[i]=scan.nextInt();
            java.util.Arrays.sort(elements);
        }
        System.out.println(java.util.Arrays.toString(elements));
    }
}
1
  • You are sorting the array inside the loop. That is a problem since your elements already has 0 set as a default value for all the array values (with the exception of the one you are setting right now or have set). This means that it's possible to overwrite a previously read value (this happens to you). Commented Sep 7, 2016 at 10:39

4 Answers 4

2

You're sorting your list everytime the loop happens which is unnecessary, instead you should use:

for (int i = 0; i < elements.length; i++) {
    elements[i]=scan.nextInt();
}
java.util.Arrays.sort(elements);

By running java.util.Arrays.sort(elements); everytime in the loop, you're essentially saying that you want to sort the elements everytime a number is entered and this is what happens:

Enter number of elements:2
Enter 2 numbers:  3
sort being called on[0, 3] 
1
sort being called on[0, 1]
[0, 1]

The reason for the 0 is that that when you initialise an int array with n number of elements you have this:

int[] x = new int[3]; // this is making [0,0,0]

So when you try and sort this after adding one element 2 -> Arrays.sort([2,0,0]) you will get [0,0,2] and hence it will cause issues.

Sign up to request clarification or add additional context in comments.

Comments

2

Doing sorting in loop will consider the default value (0) in elements as another value. Thus after sorting, the minimum value in array will be 0 which should not be.

Solution, do sorting outside loop.

for (int i = 0; i < elements.length; i++) {
    elements[i]=scan.nextInt();
}
java.util.Arrays.sort(elements);

Comments

2
Issue i see is You are sorting array even when your still getting input from user. 
e.g.
You initialized array to size 3 [0,0,0]
in first loop user provides 4  [4,0,0] after sort [0,0,4]
in Second loop user provides 7 [0,7,4] after sort [0,4,7]
in third loop user provides 1 [0,4,1]  after sort [0,1,4]
here your value is getting overwritten.  

write sort function after you finish getting input from user. 

Comments

0

There are two things that need to be noted to understand why your code is not working.

  1. Arrays are initialized with default values on creation.
  2. The call to sort is placed inside the for loop which does a sort at every step.

Explanation:

Whenever you provide "number of elements" = 2, the element is read for position elements[0] and on call to sort statement, it gets placed at position elements[1]. Then, in the next iteration, the element is read and placed at position elements[1], overwriting the value at the position. Hence, you always land up with a zero and second input number with number of elements =2.

A similar case will happen with more than two elements, where one or more elements may get overwritten.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.