1.直接选择排序
1 void selectSort(int *arr, int n) 2 { 3 int i, j; 4 5 for(i = 0; i < n; i++) 6 { 7 int temp = i; 8 9 for(j = i + 1; j < n; j++) 10 { 11 if(arr[j] < arr[temp]) 12 { 13 temp = j; 14 } 15 } 16 17 if(i != temp) 18 { 19 _swap(arr[i], arr[temp]); 20 } 21 } 22 23 }
2.堆排序
1 void makeHeap(int *arr, int i, int n) 2 { 3 while(i >= 0) 4 { 5 int left = 2 * i + 1; 6 int right = 2 * i + 2; 7 8 int temp = -1; 9 if(right < n) 10 { 11 temp = arr[right] > arr[left] ? right : left; 12 } 13 else 14 { 15 temp = left; 16 } 17 18 if(arr[temp] > arr[i]) 19 { 20 _swap(arr[i], arr[temp]); 21 } 22 23 --i; 24 } 25 } 26 27 void heapSort(int *arr, int n) 28 { 29 makeHeap(arr, n / 2 - 1, n); 30 for(int i = n - 1; i > 0; i--) 31 { 32 _swap(arr[0], arr[i]); 33 makeHeap(arr, i / 2 - 1, i); 34 } 35 }