前言
记录并分析常用排序算法,方便自己日后查阅。
环境
语言:C# IDE:Rider 2019.3.3
冒泡排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public static void BubbleSort(int[] array, int count) { bool shouldSorted = true;
for (int i = 0; i < count && shouldSorted; i++) { shouldSorted = false; for (int j = count - 1; j > i; j--) { if (array[j - 1] > array[j]) { shouldSorted = true; Utilities.Swap(ref array[j - 1], ref array[j]); } } } }
|
选择排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
public static void SelectSort(int[] array, int count) { int min; for (int i = 0; i < count - 1; i++) { min = i; for (int j = i + 1; j < count; j++) { if (array[min] > array[j]) { min = j; } }
if (min != i) { Utilities.Swap(ref array[min], ref array[i]); } } }
|
插入排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public static void InserSort(int[] array, int count) { int guard; for (int i = 0; i < count - 1; i++) { if (array[i] > array[i + 1]) { guard = array[i + 1]; int j; for (j = i; array[j] > guard && j >= 0; j--) { array[j + 1] = array[j]; }
array[j + 1] = guard; } } }
|
希尔排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
public static void ShellSort(int[] array, int count) { int i, j, guard; int increment = count; do { increment = increment / 3 + 1; for (i = increment + 1; i < count; i++) { if (array[i] < array[i - increment]) { guard = array[i]; for (j = i - increment; j >= 0 && guard < array[j]; j -= increment) { array[j + increment] = array[j]; }
array[j + increment] = guard; } } } while (increment > 1); }
|
堆排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
public static void HeapSort(int[] array, int count) { for (int i = count / 2 - 1; i >= 0; i--) { HeapAdjust(array, i, count - 1); }
for (int i = count - 1; i > 0; i--) { Utilities.Swap(ref array[0], ref array[i]); HeapAdjust(array, 0, i - 1); } }
public static void HeapAdjust(int[] array, int startIndex, int endIndex) { int temp; temp = array[startIndex]; for (int i = 2 * startIndex + 1; i <= endIndex; i = i * 2 + 1) { if (i < endIndex && array[i] < array[i + 1]) { ++i; }
if (temp > array[i]) { break; }
array[startIndex] = array[i]; startIndex = i; }
array[startIndex] = temp; }
|
归并排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
public static void MergeSort(int[] array, int count) { int[] tempArray = new int[array.Length]; int k = 1; while (k < count) { MergePass(array, tempArray, k, count); k = 2 * k; MergePass(tempArray, array, k, count); k = 2 * k; } }
public static void MergePass(int[] sr, int[] tr, int srChildLength, int arrayLength) { int hasMergeCount = 1; while (arrayLength - hasMergeCount + 1 >= 2 * srChildLength) { Merge(sr, tr, hasMergeCount - 1, hasMergeCount + srChildLength - 2, hasMergeCount + 2 * srChildLength - 2); hasMergeCount += 2 * srChildLength; }
if (arrayLength - hasMergeCount + 1 > srChildLength) { Merge(sr, tr, hasMergeCount - 1, hasMergeCount + srChildLength - 2, arrayLength - 1); } else { for (int j = hasMergeCount - 1; j < arrayLength; j++) { tr[j] = sr[j]; } } }
private static void Merge(int[] sr, int[] tr, int sr1StartIndex, int sr1EndIndex, int sr2EndIndex) { int sr2StartIndex, currentProcess;
for (sr2StartIndex = sr1EndIndex + 1, currentProcess = sr1StartIndex; sr1StartIndex <= sr1EndIndex && sr2StartIndex <= sr2EndIndex; currentProcess++) { if (sr[sr1StartIndex] < sr[sr2StartIndex]) { tr[currentProcess] = sr[sr1StartIndex++]; } else { tr[currentProcess] = sr[sr2StartIndex++]; } }
if (sr1StartIndex <= sr1EndIndex) { for (int l = 0; l <= sr1EndIndex - sr1StartIndex; l++) { tr[currentProcess + l] = sr[sr1StartIndex + l]; } }
if (sr2StartIndex <= sr2EndIndex) { for (int l = 0; l <= sr2EndIndex - sr2StartIndex; l++) { tr[currentProcess + l] = sr[sr2StartIndex + l]; } } }
|
快速排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
public static void QuickSort(int[] array, int count) { QSort(array, 0, count - 1); }
private static void QSort(int[] array, int low, int high) { int pivot;
while (low < high) { pivot = Partition(array, low, high); QSort(array, low, pivot - 1); low = pivot + 1; } }
private static int Partition(int[] array, int low, int high) { int pivotkey; int m = low + (high - low) / 2; if (array[low] > array[high]) { Utilities.Swap(ref array[low],ref array[high]); } if (array[m] > array[high]) { Utilities.Swap(ref array[m],ref array[high]); } if (array[m] > array[low]) { Utilities.Swap(ref array[low],ref array[m]); } pivotkey = array[low]; int pivotkeyback = pivotkey; while (low < high) { while (low < high && array[high] >= pivotkey) { high--; } array[low] = array[high]; while (low < high && array[low] <= pivotkey) { low++; } array[high] = array[low]; } array[low] = pivotkeyback; return low; }
|
各种排序时空复杂度
- n: 数据规模
- k: “桶”的个数
- In-place: 占用常数内存,不占用额外内存
- Out-place: 占用额外内存
此部分数据来自:https://blog.csdn.net/weixin_41190227/article/details/86600821