first, the basic concept
first, the smallest (large) element is found in the unordered sequence, which is placed at the starting position of the sort sequence (note: to exchange the largest and smallest elements and the starting elements),
then, continue looking for the smallest (large) element from the remaining unsorted elements, and then place it at the end of the sorted sequence. And so on until all elements are Sorted.
second, the characteristics of the algorithm
Is stable: false
Average Time complexity: O (n^2)
Worst Time complexity: O (n^2)
Optimal time complexity: O (n^2)
packagecom.jiafuwei.java;/*** Java Selection sort *@authorJFW **/ public classSelectionsort { public Static voidmain (string[] Args) {int[] collections = {36,23,56,789,342,456,34,7,234,4578,32}; Selectionsort Selectionsort=NewSelectionsort (); Selectionsort.sort (collections); for(inti = 0; I < collections.length; i++) {System.out.print (collections[i]+" "); } } public voidSortint[] Data) { for(intk=0;k<data.length;k++){ intMin =k; //Compare the first number in the loop with the number in the back if it is greater than the next number and then record the subscript value of the subsequent Number.//notice here that the i=k is starting from behind the K loop . for(inti=k;i<data.length;i++){ if(data[min]>data[i]) {min=i; } } //record a number in this loop. inttemp =data[k]; //turn a number in this loop into the smallest numberdata[k] =data[min]; //change the position of the previous minimum number to the first number at that timedata[min] =temp; } }}
Java algorithm (i) Direct selection sorting