Selection sorting algorithm in java


private static void selectsort(int[] array) { int n = array.length; for(int i=0;i<n;i++) { int k = i; // Find the subscript of the minimum value for ( int j = i + 1; j <n; j ++ ) { if(array[k] > array[j]) { k=j; } } // Place the minimum value in the first position of unsorted records if (k> i) { int temp = array[i]; array[i] = array[k]; array[k] = temp; } } } public static void main(String[] args) { int[] array = {100,45,17,36,21,17,13,7}; System.out.println ( "The length of the array:" + array.length); System.out.println ( "Array before sorting:" + Arrays.toString (array)); selectsort(array); System.out.println ( "sorted array:" + Arrays.toString (array)); for(int i : array) { System.out.print( i + " "); } }

 

Guess you like

Origin www.cnblogs.com/suyun0702/p/10876942.html