java实现:
1 package 排序; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 int[] buff = {5, 6, 2, 3, 8, 7}; 7 //冒泡 8 // BubleSort(buff); 9 //快排 10 // QuickSort(buff, 0, buff.length - 1); 11 //直接插入 12 // InsertSort(buff); 13 //选择排序 14 // SelectSort(buff); 15 //归并排序 16 // MergeSort(buff, 0, buff.length - 1); 17 //希尔排序 18 ShellSort(buff); 19 for(int i = 0; i < buff.length; i++) { 20 System.out.print(buff[i] + ","); 21 } 22 } 23 24 //希尔排序 25 private static void ShellSort(int[] buff) { 26 //gap是希尔间隔 27 for(int gap = buff.length / 2; gap > 0; gap /= 2) { 28 //按间隔分组进行直接插入排序 29 for(int i = gap; i < buff.length; i++) { 30 int tmp = buff[i], j = i - gap; 31 for( ; j >= 0 && buff[j] >= tmp; j -= gap) { 32 buff[j + gap] = buff[j]; 33 } 34 buff[j + gap] = tmp; 35 } 36 } 37 } 38 39 //归并排序 40 private static void MergeSort(int[] buff, int low, int high) { 41 if(low < high) { 42 int mid = (low + high) / 2; 43 MergeSort(buff, low, mid); 44 MergeSort(buff, mid + 1, high); 45 Merge(buff, low, mid, high); 46 } 47 } 48 private static void Merge(int[] buff, int low, int mid, int high) { 49 int[] tmp = new int[buff.length]; 50 //i是第一个小组的下标,j是第二个小组的下标,k是结果数组的下标 51 int i, j, k; 52 //辅助数组 53 for(k = low; k <= high; k++) { 54 tmp[k] = buff[k]; 55 } 56 //将原数组分成两组进行归并,这两组用两个下标来掌控 57 for(i = low, j = mid + 1, k = i; i <= mid && j <= high; k++) { 58 if(tmp[i] <= tmp[j]) { 59 buff[k] = tmp[i++]; 60 } 61 else { 62 buff[k] = tmp[j++]; 63 } 64 } 65 //如果两组中有没归并完的,则依次放入结果数组中 66 while(i <= mid) { 67 buff[k++] = tmp[i++]; 68 } 69 while(j <= high) { 70 buff[k++] = tmp[j++]; 71 } 72 } 73 74 //选择排序 75 private static void SelectSort(int[] buff) { 76 for(int i = 0; i < buff.length - 1; i++) { 77 int mi_pos = i; 78 //从未排序的序列中找出最小的 79 for(int j = i + 1; j < buff.length; j++) { 80 if(buff[j] < buff[mi_pos]) { 81 mi_pos = j; 82 } 83 } 84 //将最小值换到前面 85 if(mi_pos != i) { 86 int tmp = buff[i]; 87 buff[i] = buff[mi_pos]; 88 buff[mi_pos] = tmp; 89 } 90 } 91 } 92 93 //直接插入 94 private static void InsertSort(int[] buff) { 95 for(int i = 1; i < buff.length; i++) { 96 int tmp = buff[i], j; 97 for(j = i; j > 0 && buff[j - 1] >= tmp; j--) { 98 buff[j] = buff[j - 1]; 99 } 100 buff[j] = tmp; 101 } 102 } 103 104 //快排 105 private static void QuickSort(int[] buff, int low, int high) { 106 if(low >= high) { 107 return; 108 } 109 int pivort = Partition(buff, low, high); 110 QuickSort(buff, low, pivort - 1); 111 QuickSort(buff, pivort + 1, high); 112 } 113 private static int Partition(int[] buff, int low, int high) { 114 //以数组第一个数作为哨兵 115 int tmp = buff[low]; 116 while(low < high) { 117 while(low < high && buff[high] >= tmp) { 118 high--; 119 } 120 buff[low] = buff[high]; 121 while(low < high && buff[low] <= tmp) { 122 low++; 123 } 124 buff[high] = buff[low]; 125 } 126 buff[low] = tmp; 127 return low; 128 } 129 130 //冒泡 131 private static void BubleSort(int[] buff) { 132 for(int i = 0; i < buff.length; i++) { 133 for(int j = 0; j < buff.length - 1; j++) { 134 if(buff[j] > buff[j + 1]) { 135 int tmp = buff[j]; 136 buff[j] = buff[j + 1]; 137 buff[j + 1] = tmp; 138 } 139 } 140 } 141 } 142 143 }