数据结构之排序技术:快速排序、归并排序、堆排序(C++版)
快速排序
1 #include <iostream> 2 using namespace std; 3 4 void swap(int num[], int i, int j) 5 { 6 int temp = num[i]; 7 num[i] = num[j]; 8 num[j] = temp; 9 } 10 11 int partition(int num[], int left, int right) 12 { 13 int key = left; 14 int value = num[key]; 15 while (left < right) 16 { 17 while (left < right && num[right] >= value)right--; 18 while (left < right && num[left] <= value)left++; 19 swap(num, right, left); 20 } 21 swap(num, key, left); 22 return left; 23 } 24 25 void QuickSort(int num[], int begin, int end) 26 { 27 if (begin < end) 28 { 29 int middle = partition(num, begin, end); 30 QuickSort(num, begin, middle - 1); 31 QuickSort(num, middle + 1, end); 32 } 33 } 34 35 int main() 36 { 37 int num[100]; 38 int n = 0; 39 while (cin >> n) 40 { 41 for (int i = 0; i < n; i++) 42 { 43 int temp = 0; 44 cin >> temp; 45 num[i] = temp; 46 } 47 QuickSort(num, 0, n - 1); 48 for (int i = 0; i < n; i++) 49 cout << num[i] << " "; 50 cout << endl; 51 } 52 return 0; 53 }
归并排序
#include <iostream> using namespace std; void Merge(int *from, int *to, int begin, int middle, int end) { int i = begin; int j = middle + 1; int k = i; while (i <= middle && j <= end) { if (from[i] < from[j]) to[k++] = from[i++]; else to[k++] = from[j++]; } while (i <= middle) to[k++] = from[i++]; while (j <= end) to[k++] = from[j++]; } void MergePass(int *from, int *to, int end, int h) { int i = 0; while (i <= end - 2 * h + 1) { Merge(from, to, i, i + h - 1, i + 2 * h - 1); i += 2 * h; } if (i < end - h + 1) Merge(from, to, i, i + h - 1, end); else for (int k = i; k <= end; k++) { to[k] = from[k]; } } void MergeSort(int *from, int *to, int begin, int end) { int h = 1; while (h <= end) { MergePass(from, to, end, h); h = 2 * h; MergePass(to, from, end, h); h = 2 * h; } } int main() { int num[100]; int num2[100]; int n = 0; while (cin >> n) { for (int i = 0; i < n; i++) { int temp = 0; cin >> temp; num[i] = temp; } MergeSort(num, num2, 0, n - 1); for (int i = 0; i < n; i++) cout << num[i] << " "; cout << endl; } return 0; }
堆排序
1 #include <stdio.h> 2 3 void HeapAdjust(int *num, int s, int length) 4 { 5 int temp = num[s]; 6 int child = 2 * s + 1; 7 while (child < length) 8 { 9 if (child + 1 < length && num[child] < num[child + 1]) 10 child++; 11 if (num[s] < num[child]) 12 { 13 num[s] = num[child]; 14 num[child] = temp; 15 s = child; 16 child = 2 * s + 1; 17 } 18 else 19 break; 20 } 21 } 22 23 void buildingHeap(int *num, int length) 24 { 25 for (int i = (length - 1) / 2; i >= 0; --i) 26 HeapAdjust(num, i, length); 27 } 28 29 void HeapSort(int *num, int length) 30 { 31 buildingHeap(num, length); 32 for (int i = length - 1; i > 0; --i) 33 { 34 int temp = num[0]; 35 num[0] = num[i]; 36 num[i] = temp; 37 HeapAdjust(num, 0, i); 38 } 39 } 40 41 void print(int num[], int n) { 42 for (int i = 0; i < n; i++) { 43 printf("%d ", num[i]); 44 } 45 printf("\n"); 46 } 47 48 int main() 49 { 50 int num[100]; 51 int n = 0; 52 while (scanf("%d", &n) != EOF) 53 { 54 for (int i = 0; i < n; i++) 55 scanf("%d", &num[i]); 56 HeapSort(num, n); 57 print(num, n); 58 } 59 return 0; 60 }