Select sort
Select sort: First Find the smallest element in the array , and second, swap it with the first element of the group (if the first element is the smallest element then swap it yourself). Again, find the smallest element in the remaining element and swap it with the second element of the array . This is repeated until the entire array is sorted. This is the selection sort, because it is the smallest of the remaining elements that are constantly selected.
Conclusion: for an array of length n, choosing a sort requires approximately N2/2 and N-Times.
Characteristics:
- run time is independent of input. scanning the array once to find the smallest element does not provide any information for the next scan. This property is a disadvantage in some cases, because the person using the selection sort may be surprised to find that an array with an ordered array or a primary key that is all equal, and an array that is randomly arranged by an element, are sorted as long as the ordering time.
- data movement is minimal. Each interchange changes the value of the elements of the two array, so the Select sort uses n-Times Exchange- the number of interchanges and the size of the arrays are linearly related.
1 Public Static voidsort (comparable[] a) {2 //sort A in ascending order3 intL =a.length;4 for(inti = 0; i < L; i++) {5 //The subscript of the minimum value is initially I6 intMin =i;7 for(intj = i + 1; J < L; J + +) {8 //Compare Size9 if(A[min].compareto (a[j]) > 0) {Ten //assigns the subscript of the minimum value to min OneMin =J; A } - } - //Place the lowest value subscript and "I" transposition (minimum value transposition) the Exch (A, I, min); - } - -}
Data structure and algorithm (i) Select sort