|
| 1 | +#include <iostream> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +template<class T> |
| 5 | +void Swap(T* arr, int v1, int v2) |
| 6 | +{ |
| 7 | + T tmp = arr[v1]; |
| 8 | + arr[v1] = arr[v2]; |
| 9 | + arr[v2] = tmp; |
| 10 | +} |
| 11 | + |
| 12 | +template<class T> |
| 13 | +void QuickSort(T* arr, int low, int high) |
| 14 | +{ |
| 15 | + if (low >= high) return; |
| 16 | + int left = low; |
| 17 | + int right = high; |
| 18 | + T key = arr[low]; |
| 19 | + while (left < right) |
| 20 | + { |
| 21 | + while (left < right && key < arr[right]) --right; |
| 22 | + if (left < right) Swap(arr, left, right); |
| 23 | + while (left < right && arr[left] < key) ++left; |
| 24 | + if (left < right) Swap(arr, left, right); |
| 25 | + } |
| 26 | + QuickSort(arr, low, left - 1); |
| 27 | + QuickSort(arr, left + 1, high); |
| 28 | +} |
| 29 | + |
| 30 | +template<class T> |
| 31 | +void InsertionSort(T* arr, int len) |
| 32 | +{ |
| 33 | + for (int i = 1; i < len; i++) |
| 34 | + { |
| 35 | + T key = arr[i]; |
| 36 | + int j = 0; |
| 37 | + for (j = i - 1; j >= 0; j--) |
| 38 | + if (key < arr[j]) |
| 39 | + arr[j + 1] = arr[j]; |
| 40 | + else break; |
| 41 | + arr[j + 1] = key; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +template<class T> |
| 46 | +void SelectionSort(T* arr, int len) |
| 47 | +{ |
| 48 | + int min = 0; |
| 49 | + for (int i = 0; i < len; i++) |
| 50 | + { |
| 51 | + min = i; |
| 52 | + for (int j = i; j < len; j++) |
| 53 | + if (arr[j] < arr[min]) min = j; |
| 54 | + Swap(arr, min, i); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +template<class T> |
| 59 | +void BubbleSort(T* arr, int len) |
| 60 | +{ |
| 61 | + for (int i = 0; i < len; i++) |
| 62 | + for (int j = 0; j < len - 1; j++) |
| 63 | + if (arr[j] > arr[j + 1]) |
| 64 | + Swap(arr, j, j + 1); |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +template <class T> |
| 69 | +void AdjustHeap(T* arr, int len, int target) |
| 70 | +{ |
| 71 | + int l = target * 2 + 1; |
| 72 | + int r = target * 2 + 2; |
| 73 | + int max = target; |
| 74 | + if (l < len && arr[l] > arr[max]) max = l; |
| 75 | + if (r < len && arr[r] > arr[max]) max = r; |
| 76 | + if (max != target) |
| 77 | + { |
| 78 | + Swap(arr, max, target); |
| 79 | + AdjustHeap(arr, len, max); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +template <class T> |
| 84 | +void BuildHeap(T* arr, int len) |
| 85 | +{ |
| 86 | + for (int i = (len - 2) / 2; i >= 0; i--) |
| 87 | + AdjustHeap(arr, len, i); |
| 88 | +} |
| 89 | + |
| 90 | +template <class T> |
| 91 | +void HeapSort(T* arr, int len) |
| 92 | +{ |
| 93 | + BuildHeap(arr, len); |
| 94 | + for (int i = len - 1; i >= 0; i--) |
| 95 | + { |
| 96 | + Swap(arr, 0, i); |
| 97 | + AdjustHeap(arr, i, 0); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +template<class T> |
| 102 | +void OutPut(T* arr, int len) |
| 103 | +{ |
| 104 | + for (int i = 0; i < len; i++) |
| 105 | + cout << arr[i] << "->"; |
| 106 | + cout << endl; |
| 107 | +} |
| 108 | + |
| 109 | +int main(int* argc, char** argv) |
| 110 | +{ |
| 111 | + int arr1[10] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; |
| 112 | + double arr2[10] = { 100.0, 99.0, 88.0, 77.0, 66.0, 55.0, 44.0, 33.0, 22.0, 11.0 }; |
| 113 | + //QuickSort(arr1, 0, 9); |
| 114 | + //InsertionSort(arr1, 10); |
| 115 | + //SelectionSort(arr1, 10); |
| 116 | + //BubbleSort(arr1, 10); |
| 117 | + HeapSort(arr1, 10); |
| 118 | + OutPut(arr1, 10); |
| 119 | + //QuickSort(arr2, 0, 9); |
| 120 | + //OutPut(arr2, 10); |
| 121 | + return 0; |
| 122 | +} |
0 commit comments