Select Sorting Algorithm and Sorting Algorithm
Select sorting: for example, in an unordered array with a length of N, traverse N data in the first row to find the minimum value and switch with the first element, the second trip traverses the remaining N-1 data and finds out the smallest value to exchange with the second element ...... the N-1 traverses the remaining 2 data, find out the smallest value exchange with the N-1 element, so now the selection is complete.
Java Implementation for sorting:
1 package com. mianshi. easy; 2 public class Selection {3 4 public static void main (String [] args) {5 int [] a = {, 7 }; 6 7 selectionSort (a); 8 9 for (int I = 0; I <. length; I ++) {10 System. out. print (a [I] + ""); 11} 12} 13 14 public static void selectionSort (int [] a) 15 {16 for (int I = 0; I <. length; I ++) 17 {18 int k = I; 19 for (int j = I + 1; j <. length; j ++) 20 {21 if (a [k]> a [j]) 22 {23 k = J; 24} 25} 26 // if k is not equal to I, it indicates that at least one element is smaller than the custom minimum value and the subscript of this element is obtained, used to exchange 27 if (k! = I) 28 {29 int temp = a [I]; 30 a [I] = a [k]; 31 a [k] = temp; 32} 33} 34} 35}
The function of k in the Code is similar to a pointer pointing to the smallest number.
Algorithm stability:An unstable sorting algorithm. The selection of sorting is unstable (for example, the first [5] and [3] of the sequence [5, 5, 3] are exchanged for the first time, cause the first 5 to move to the second 5 ).
Time Complexity:Average time complexity is