Home > Net >  java - Selection Sort
java - Selection Sort

Time:09-29

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

}

CodePudding user response:

You should set

int j = 0

in the inner for

CodePudding user response:

If you want to implement Selctionsort

  • initialize the inner for with int j = i 1
  • change numbers if array[i] is greater than array[j] not the other way around
        for (int i =0;i <array.length;i  ) {
            int minValue = array[i];

            for (int j = i  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] " ");
        }

CodePudding user response:

You need to read the entire array in first, then you can sort it. Also, I don't consider your algorithm a true selection sort. In selection sort, you must find the minimum in the array of unsorted data. Then you swap. Your algorithm doesn't do that exactly.

To illustrate, I have broken the code into functions.

// Find the minimum value in the array, starting the search at "start"
// Returns the index of the minimum
static int findMinIndex(int[] array, int start)
{
    int min = array[start];
    int minIndex = start;
    for (int i = start   1; i < array.length; i  ) {
        if (array[i] < min)  {
            min = array[i];
            minIndex = i;
        }
    }
    return minIndex;
}

// Swap 2 elements of an array
static void swap(int[] array, int index1, int index2)
{
    int temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;
}

// Selection sort the array, ascending
static void selectionSort(int[] array)
{
    for (int i = 0; i < array.length; i  ) {
        // First find the minimum from i to the end of the array...
        int minIndex = findMinIndex(array, i);
        // ...then swap
        if (minIndex != i) {
            swap(array, i, minIndex);
        }
    }
}
  • Related