Select basic sorting idea
Bubble Sorting has a disadvantage. For example, when we compare the first number of a1 and the second number of a2, positions will be exchanged as long as a1 is larger than a2, but we cannot determine that a2 is the smallest element, if there is something smaller than it, the element will be exchanged with a2 again, and this exchange may occur multiple times to determine the final position of a2.
Selecting sorting can avoid this time-consuming switching operation. Starting from the first element, scan the entire array to be arranged, find the smallest element, and then swap the position with the first element, then, starting from the second element, continue to find the smallest element and the second element exchange position, and so on.
Java Implementation
// Select the sort public void selectionSort () {int minPoint; // small int len = array that stores the smallest element. length; int temp; int counter = 1; for (int I = 0; I
Algorithm Analysis
Select sorting is the same as Bubble sorting.N * (N-1)/2Only N exchanges are required. When N is large, the time of the number of exchanges has a greater influence.Select the time complexity of sorting as O (N2).
Although the selected sorting and Bubble Sorting belong to the same order of magnitude in time complexity, there is no doubt that the efficiency of the selected sorting is higher, because it has fewer switching operations, in addition, when the switching operation is much longer than the comparison operation time, the sorting speed is quite fast.
Improved sorting
The traditional sorting method only determines the minimum value each time. Based on the experience of the improved bubble algorithm, we can improve the sorting algorithm as follows: determine the maximum and minimum values for each sort, in this way, the number of sorted shards can be reduced by half.
The improved code is as follows:
// Select public void selectionSort_improvement () {int minPoint; // the smallest int maxPoint for storing the smallest element; // the smallest int len = array for storing the largest element. length; int temp; int counter = 1; for (int I = 0; I
Array [maxPoint]) {// if an element in the array to be sorted is larger than the current element, maxPoint points to the subscript of this element maxPoint = j ;}} if (minPoint! = I) {// If a smaller element is found, switch the location temp = array [I] with the first element; array [I] = array [minPoint]; array [minPoint] = temp; // The original first element has exchanged the position with the element whose subscript is minPoint. // If maxPoint points to the first element, you need to re-point maxPoint to array [minPoint] // because the current array [minPoint] stores the data in the previous first element if (maxPoint = I) {maxPoint = minPoint ;}} if (maxPoint! = Len-1-i) {// if a larger element is found, swap the location temp = array [len-1-i]; array [len-1-i] = array [maxPoint] with the last element; array [maxPoint] = temp;} System. out. print (+ counter + round sorting result :); display (); counter ++ ;}}