public void test() { //-------------直接插入排序算法 Console.WriteLine("---------直接插入排序算法------------ "); showArray(); // --------------归并排序算法 Console.WriteLine("---------归并排序算法------------ "); showArray(); // --------------直接选择排序算法 Console.WriteLine("---------直接选择排序算法------------ "); showArray(); //--------------冒泡排序算法 Console.WriteLine("---------冒泡排序算法 ------------ "); showArray(); } private void showArray() { int[] arr; int[] sorted = null; arr = BuilderArray.getArray(1000, 5000); Console.WriteLine("数组大小:- " + arr.Length + "- 元素为:"); int index = 1; for (int k = 0; k < arr.Length; k++) { Console.Write(arr[k] + " ,"); if (index % 10 == 0) Console.WriteLine(); index++; } DateTime begin = DateTime.Now; sorted = Sorted.insertSort(arr); Console.WriteLine(" ----- 排序后: : "); index = 1; for (int m = 0; m < sorted.Length; m++) {// 迭代 Console.Write(sorted[m] + " ,"); if (index % 15 == 0) Console.WriteLine(); index++; } DateTime end = DateTime.Now; Console.WriteLine(" -----排序耗时(s) :" + (( end-begin).Milliseconds+"ms")); Console.ReadKey(); } public class BuilderArray { /** * 获得随机大小数组方法 * @param max_index 最大索引 * @param max_value 最大值 * @return disorder 返回数组 */ public static int[] getArray(int max_index, int max_value) { Random random = new Random(); int index = random.Next(max_index); int[] disorder = new int[index];//初始化一个随机大小的数组对象 for (int i = 0; i < index; i++) { disorder[i] = random.Next(max_value); } return disorder; } } public class Sorted { /** * @category 归并排序算法 * @param disorder 未排序数组 * @param order 已排序数组 * @param disorder_length 未排序数组长度 * @return 排序数组 */ public static int[] mergeSort(int[] disorder, int[] order, int disorder_length) { int h = 1; while (h < disorder_length) { mergeOrder(disorder, order, disorder_length - 1, h); h = 2 * h; mergeOrder(order, disorder, disorder_length - 1, h); h = 2 * h; } return order; } /** * @category 归并排序算法 * @param disorder 未排序 * @param order 已排序 * @param left 左边索引 * @param right 右边索引 * @param end 末尾索引 */ private static void merge(int[] disorder, int[] order, int left, int right, int end) { int i = left, j = right + 1, k = left; while (i <= right && j <= end) { if (disorder[i] <= disorder[j]) { order[k++] = disorder[i++]; // 取r[i]和r[j]中较小者放入r1[k] } else { order[k++] = disorder[j++]; } } if (i <= right) { while (i <= right) { // 若第一个子序列处理完,则进行收尾处理 order[k++] = disorder[i++]; } } else { while (j <= end) { // 若第二个子序列处理完,则进行收尾处理 order[k++] = disorder[j++]; } } } /** * @category 归并排序算法 * @param disorder 未排序 * @param order 已排序 * @param disorder_length 未排序数组长度 * @param h */ private static void mergeOrder(int[] disorder, int[] order, int disorder_length, int h) { int i = 0; while (i <= disorder_length - 2 * h) { //待归并记录至少有两个长度为h的子序列 merge(disorder, order, i, i + h - 1, i + 2 * h - 1); i += 2 * h; } if (i < disorder_length - h) { merge(disorder, order, i, i + h - 1, disorder_length); //待归并序列中有一个长度小于h } else { for (int k = i; k <= disorder_length; k++) { //待归并序列中只剩一个子序列 order[k] = disorder[k]; } } } /** * @category 快速排序算法 */ /** * @category 直接插入排序算法 * @param disorder 数组 */ public static int[] insertSort(int[] disorder) { int temp = 0;//临时变量 for (int i = 1; i < disorder.Length; i++) { int j = i - 1; temp = disorder[i]; for (; j >= 0 && temp < disorder[j]; j--) { disorder[j + 1] = disorder[j];//替换位置 } disorder[j + 1] = temp; } return disorder; } /** * @category 直接选择排序算法 * @param disorder 数组 */ public static int[] selectSort(int[] disorder) { int i, j, index, temp; for (i = 0; i < disorder.Length; i++) { index = i; //初始化第i趟选择排序的最小记录的指针 for (j = i + 1; j < disorder.Length; j++) { //在无序区选取最小记录 if (disorder[j] < disorder[index]) { index = j; } } if (index != i) { //将最小记录与r[i]交换 temp = disorder[i]; disorder[i] = disorder[index]; disorder[index] = temp; } } return disorder; } /** * @category 冒泡排序算法(逻辑简单,适用性不强) * @param disorder 数组 */ public static int[] bubbleSort(int[] disorder) { int temp = 0; for (int i = 0; i < disorder.Length; i++) { for (int j = 0; j < disorder.Length - i - 1; j++) if (disorder[j] > disorder[j + 1]) { // 进行两两比较,下面进行符合条件的交换 temp = disorder[j + 1]; disorder[j + 1] = disorder[j]; disorder[j] = temp; } } return disorder; } }