Search code examples
javaeclipseselection-sort

java - Selection Sort


I want to implement a selection alignment that receives 10 integers and organizes them in ascending order.

However, when my code is operated, other things work normally, but only the first integer is not aligned.

Please let me know how to fix the code.

public static void sort(int[] array) {
    Scanner sc = new Scanner(System.in);
    System.out.println("put the int");
    
    for (int i =0;i <array.length;i++) {
        System.out.print((i+1)+": ");
    int n = sc.nextInt();
        array[i] = n;
        for (int j = 1; j < array.length;j++) {
            if (array[i] < array[j]) {
                
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
    
    for (int a=0; a< array.length; a++) {
        System.out.print(array[a]+" ");

    }
}
public static void main(String[] args) {
    int[] my_array = {0,0,0,0,0,0,0,0,0,0};
    sort(my_array);
}

}


Solution

  • You should set

    int j = 0
    

    in the inner for