1. Algorithm description
Select sorting: For example, in an unordered array of length N, traverse N data in the first trip, find out the smallest value and exchange it with the first element, the second trip traverse the remaining N-1 data, find out the smallest value and exchange it with the second element... The N-1st traverses the remaining two data, find out the smallest value and exchange it with the N-1th element, and the selection of sorting is completed.
The following 5 unordered data are used as examples:
56 12 80 91 20 (The selection process of the first trip is only refined in the article)
1st trip: 12 56 80 91 20
2nd trip: 12 20 80 91 56
Trip 3: 12 20 56 91 80
4th trip: 12 20 56 80 91
2. Algorithm analysis
Average time complexity: O(n2)
Space complexity: O(1) (for exchange and record indexes)
Stability: Unstable (for example, the first [5] and [3] are exchanged on the first trip of the sequence [5, 5, 3], causing the first 5 to move behind the second 5)
3. Algorithm implementation
public class SelectionSort { public static void main(String[] args) { int len = 15; int[] ary = new int[len]; Random random = new Random(); for (int j = 0; j < len; j++) { ary[j] = random.nextInt(1000); } System.out.println("------------------"); // ary=new int[]{10,9,8,7,6,5,4,3,2,1}; // test exchanges// ary=new int[]{1,2,3,4,5,6,7,8,10,9}; //Number of test exchanges for (int j = 0; j < ary.length; j++) { System.out.print(ary[j] + " "); } selectDesc(ary); selectAsc(ary); } /* * Select sort: descending */ static void selectDesc(int[] ary) { int compareCount = 0;//Number of comparison int changeCount = 0;//Number of exchanges int len = ary.length; int maxValueIndex = -1; //Record the minimum value index after a round of comparison for (int i = 0; i < len - 1; i++) { maxValueIndex = i; //From 0 for (int j = i + 1; j < len; j++) { if (ary[maxValueIndex] < ary[j]) { maxValueIndex = j; //Record larger index compareCount++; } } // System.out.println("minValueIndex==" + maxValueIndex); if (maxValueIndex != i) {//If the index is different from the record on the left, exchange ary[i] = ary[maxValueIndex] + (ary[maxValueIndex] = ary[i]) * 0;//One-step exchange changeCount++; } } System.out.println("/n------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- (int i = 0; i < len - 1; i++) { minIndex = i; for (int j = i + 1; j < len; j++) { if (ary[minIndex] > ary[j]) { minIndex = j; //Record smaller index compareCount++; } } if (minIndex != i) {//If the index is different from the record on the left, then exchange ary[i] = ary[minIndex] + (ary[minIndex] = ary[i]) * 0; changeCount++; } } System.out.println("/n---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- compareCount + ", number of exchanges" + changeCount); for (int j = 0; j < ary.length; j++) { System.out.print(ary[j] + " "); } } }--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------