常用排序算法模板
Posted onlyblues
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常用排序算法模板相关的知识,希望对你有一定的参考价值。
这里就不再介绍算法的实现原理,直接给出常用的排序算法模板,包括冒泡排序,插入排序,希尔排序,选择排序,堆排序,归并排序,快速排序。这些代码都是我个人常用的模板,代码也比较的简短精炼。若解题有需要,直接套模板即可。
冒泡排序(Bubble Sort)
1 void bubbleSort(int *a, int n) { 2 for (int i = 0; i < n - 1; i++) { 3 bool flag = false; 4 for (int j = 0; j < n - 1 - i; j++) { 5 if (a[j] > a[j + 1]) { 6 std::swap(a[j], a[j + 1]); 7 flag = true; 8 } 9 } 10 if (flag == false) break; 11 } 12 }
插入排序(Insertion Sort)
1 void insertionSort(int *a, int n) { 2 for (int i = 1; i < n; i++) { 3 int tmp = a[i], j = i; 4 for ( ; j > 0 && tmp < a[j - 1]; j--) { 5 a[j] = a[j - 1]; 6 } 7 a[j] = tmp; 8 } 9 }
希尔排序(Shell Sort)
1 void shellSort(int *a, int n) { 2 for (int k = n >> 1; k; k >>= 1) { 3 for (int i = k; i < n; i++) { 4 int tmp = a[i], j = i; 5 for ( ; j >= k && tmp < a[j - k]; j -= k) { 6 a[j] = a[j - k]; 7 } 8 a[j] = tmp; 9 } 10 } 11 }
选择排序(Selection Sort)
1 void selectSort(int *a, int n) { 2 for (int i = 0; i < n; i++) { 3 int minId = i; 4 for (int j = i + 1; j < n; j++) { 5 if (a[j] < a[minId]) minId = j; 6 } 7 std::swap(a[i], a[minId]); 8 } 9 }
堆排序(Heap Sort)
1 void percDown(int *a, int n, int pos) { 2 int tmp = a[pos], parent = pos; 3 for (int child = parent * 2 + 1; child < n; parent = child, child = parent * 2 + 1) { 4 if (child != n - 1 && a[child + 1] > a[child]) child++; 5 if (tmp >= a[child]) break; 6 else a[parent] = a[child]; 7 } 8 a[parent] = tmp; 9 } 10 11 void heapSort(int *a, int n) { 12 for (int i = n / 2; i >= 0; i--) { 13 percDown(a, n, i); 14 } 15 for (int i = n - 1; i > 0; i--) { 16 std::swap(a[0], a[i]); 17 percDown(a, i, 0); 18 } 19 }
归并排序(Merge Sort)
递归版本:
1 void mergeSort(int *a, int low, int high) { 2 if (low >= high) return; 3 int mid = low + high >> 1; 4 mergeSort(a, low, mid); 5 mergeSort(a, mid + 1, high); 6 7 int i = low, j = mid + 1, k = 0; 8 int tmp[high - low + 1]; 9 while (i <= mid && j <= high) { 10 if (a[i] <= a[j]) tmp[k++] = a[i++]; 11 else tmp[k++] = a[j++]; 12 } 13 while (i <= mid) { 14 tmp[k++] = a[i++]; 15 } 16 while (j <= high) { 17 tmp[k++] = a[j++]; 18 } 19 for (int i = 0; i < k; i++) { 20 a[low + i] = tmp[i]; 21 } 22 }
用STL中的std::merge,来减少代码量:
1 void mergeSort(int *a, int low, int high) { 2 if (low >= high) return; 3 int mid = low + high >> 1; 4 mergeSort(a, low, mid); 5 mergeSort(a, mid + 1, high); 6 7 int tmp[high - low + 1]; 8 std::merge(a + low, a + mid + 1, a + mid + 1, a + high + 1, tmp); 9 std::copy(tmp, tmp + high - low + 1, a + low); 10 }
非递归版本:
1 void mergeSort(int *a, int n) { 2 int tmp[n]; 3 for (int len = 1; len < n; len <<= 1) { 4 int mid, right; 5 for (int left = 0; left < n - len; left = right) { 6 mid = left + len, right = std::min(mid + len, n); 7 std::merge(a + left, a + mid, a + mid, a + right, tmp + left); 8 } 9 std::copy(tmp, tmp + right, a); 10 } 11 }
快速排序(Quick Sort)
1 void qSort(int *a, int low, int high) { 2 if (low >= high) return; 3 int pivot = a[low + high >> 1]; 4 int i = low - 1, j = high + 1; 5 while (i < j) { 6 while (a[++i] < pivot); 7 while (a[--j] > pivot); 8 if (i < j) std::swap(a[i], a[j]); 9 } 10 qSort(a, low, j); 11 qSort(a, j + 1, high); 12 }
以上是关于常用排序算法模板的主要内容,如果未能解决你的问题,请参考以下文章