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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
#region 排序方法 #region 冒泡排序法 /// <summary> /// 冒泡排序法 /// </summary> /// <param name="list">数据列表</param> /// <param name="SortType">排序类型,选择是升序还是降序</param> public static void BubbleSort(int[] list, string SortType) { int j, temp; j = 1; while ((j < list.Length)) { for (int i = 0; i < list.Length - j; i++) { bool Bl; if (SortType == "asc") { Bl = list[i] > list[i + 1]; } else if (SortType == "desc") { Bl = list[i] < list[i + 1]; } else { Bl = false; } if (Bl) { temp = list[i]; list[i] = list[i + 1]; list[i + 1] = temp; } } j++; } } #endregion 冒泡排序法 #region 选择排序法 /// <summary> /// 选择排序法 /// </summary> /// <param name="list">数据列表</param> public static void ChoiceSort(int[] list) { int min; for (int i = 0; i < list.Length - 1; i++) { min = i; for (int j = i + 1; j < list.Length; j++) { if (list[j] < list[min]) min = j; } int t = list[min]; list[min] = list[i]; list[i] = t; } } #endregion 选择排序法 #region 插入排序法 /// <summary> /// 插入排序法 /// </summary> /// <param name="list">数据列表</param> public static void InsertSort(int[] list) { for (int i = 1; i < list.Length; i++) { int t = list[i]; int j = i; while ((j > 0) && (list[j - 1] < t)) { list[j] = list[j - 1]; --j; } list[j] = t; } } #endregion 插入排序法 #region 希尔排序法 /// <summary> /// 希尔排序法 /// </summary> /// <param name="list">数据列表</param> public static void ShellSort(int[] list) { int inc; for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ; for (; inc > 0; inc /= 3) { for (int i = inc + 1; i <= list.Length; i += inc) { int t = list[i - 1]; int j = i; while ((j > inc) && (list[j - inc - 1] > t)) { list[j - 1] = list[j - inc - 1]; j -= inc; } list[j - 1] = t; } } } #endregion 希尔排序法 #endregion 排序方法 |