Sorting algorithm (iii): Simple selection sorting algorithm-java implementation
The simple selection algorithm is another sort of sorting algorithm in addition to the heap ordering, it is also an unstable sorting algorithm, the average time complexity is O (N2), the space time complexity is O (1)
Simple selection algorithm is relatively simple, and easy to understand, the specific sorting algorithm ideas are as follows:
- Select the smallest element in the array to swap with array[i], always dividing the array into aray[0]...array[i] ordered and array[i+1]...array[array.length-1] unordered two parts
- Place the smallest value in the unordered order at Array[i+1]
The implementation code is as follows:
1 Public classSelectsort {2 3 Public Static voidMain (string[] args) {4 int[] Array =New int[]{5, 3, 6, 2, 1, 9, 4, 8, 7};5Selectsort Selectsort =NewSelectsort ();6 Selectsort.selectsort (array);7 for(inti = 0; i < Array.Length; i++) {8System.out.print (Array[i] + "");9 }Ten } One A Public voidSelectsort (int[] Array) { - if(Array = =NULL|| Array.Length = = 0)return; - the for(inti = 0; i < Array.Length; i++) { - //default minimum is I - intMin =i; - //This loop is primarily a selection of the smallest element + for(intj = i + 1; J < Array.Length; J + +) { - if(Array[min] >Array[j]) { +Min =J; A } at } - - //swap two elements - if(min! =i) { - Swap (array, I, min); - } in - } to } + - Private voidSwapint[] Array,intIintj) { the //there may be overflow *Array[i] = Array[i] +Array[j]; $ARRAY[J] = array[i]-Array[j];Panax NotoginsengArray[i] = array[i]-Array[j]; - } the}
Sorting algorithm (iii): Simple selection sorting algorithm-java implementation