user6804120
user6804120

Reputation:

Printing sorted array in Java

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));
    }
}

Upvotes: 0

Views: 1647

Answers (4)

bhrt
bhrt

Reputation: 72

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.

Upvotes: 0

Rahul Sawant
Rahul Sawant

Reputation: 1264

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. 

Upvotes: 2

Shahid
Shahid

Reputation: 2330

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);

Upvotes: 2

px06
px06

Reputation: 2326

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.

Upvotes: 2

Related Questions