1. Algorithm Description
Select sort: In an unordered array with a length of N, traverse n data in the first row, find the minimum value of which is exchanged with the first element, and traverse the remaining N-1 data in the second row, find the minimum value and switch it to 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.
Ii. Algorithm Analysis
Average time complexity: O (n2)
Spatial complexity: O (1) (used for exchange and record indexing)
Stability: unstable (for example, in sequence [5, 5, 3], the first step is to exchange the first [5] with [3], resulting in the first 5 moving to the second 5)
3. Algorithm Implementation
1 #include<stdio.h> 2 int main() 3 { 4 int array[] = {3, 1, 5, 2, 6 ,4}; 5 int count = sizeof(array) / sizeof(array[0]); 6 for (int i = 0; i < count - 1; i++) { 7 int minIndex = i; 8 for (int j = minIndex + 1; j < count; j++) { 9 if (array[minIndex] > array[j]) {10 minIndex = j;11 }12 }13 if (minIndex != i) {14 int temp = 0;15 temp = array[minIndex];16 array[minIndex] = array[i];17 array[i] = temp;18 }19 }20 for (int i = 0; i < count; i++) {21 printf("%d\n", array[i]);22 }23 return 0;24 }