C#_基础:排序算法
//希尔排序 static int[] ShellSort(int[] array) { if (array != null) { int[] list = { 9, 5, 3, 2, 1 }; foreach (int k in list) { for (int i = k; i < array.Length; i++) { int current = array[i]; int preIndex = i - k; while (preIndex >= 0 && preIndex < array.Length && current < array[preIndex]) { array[preIndex + k] = array[preIndex]; preIndex = preIndex - k; } array[preIndex + k] = current; } } } return array; } //插入排序 public int[] InsertSort(int[] array) { if (array != null) { int k = 1;//使用k变量,后面更好的扩展到Shell排序 for (int i = k; i < array.Length; i++) { int current = array[i]; int preIndex = i - k; while (preIndex >= 0 && preIndex < array.Length && current < array[preIndex]) { array[preIndex + k] = array[preIndex]; preIndex = preIndex - k; } array[preIndex + k] = current; } } return array; } //冒泡排序 public int[] BubbleSort(int[] array) { if (array != null) { for (int i = 0; i < array.Length; i++) { for (int j = 1; j < array.Length - i; j++) { if (array[j - 1] > array[j]) { int temp = array[j - 1]; array[j - 1] = array[j]; array[j] = temp; } } } } return array; }