package paixu; public class paixu { public static void main(String[] args) { sort qs = new sort(); int i[] = { 49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51 }; qs.quiteSort(i, 0, i.length - 1); // qs.bubblesort(i); // int[] spans = {5,3,1}; // qs.shellSort(i, spans); // qs.insertSort(i); // qs.selectSort(i); // qs.heapSort(i); // qs.mergeSort(i, 0, i.length - 1); // qs.radixSort(i, 10); for (int data : i) { System.out.print(data + " "); } } } /* 程序员必知的8大内部排序 * 插入排序:直接插入&希尔 * 选择排序:直接选择&堆 * 交换排序:冒泡&快速 * 归并排序 * 基数排序 */ class sort { /* 快速排序 * 选出基准项, * 将小的放前面,大的放后面, * 然后分别对前面和后面进行嵌套执行*/ public void quiteSort(int data[], int low, int hight) { if (low < hight) { int pivotpos = partition(data, low, hight); quiteSort(data, low, pivotpos - 1); quiteSort(data, pivotpos + 1, hight); } } private int partition(int[] data, int low, int high) {// 选定枢轴为low所对应的值 int pivot = data[low]; while (low < high) { while (low < high && pivot <= data[high]) {// 高位找到比povite大,则符合要求,继续寻找 high--; } data[low] = data[high]; while (low < high && data[low] <= pivot) { // 低位开始找到比povite小,符合要求,继续寻找 low++; } data[high] = data[low]; } data[low] = pivot; return low; } /* 冒泡排序 * 扫描数据,寻找相邻乱序的项, * 交换项的位置然后继续扫描。 * 乱序少,快;乱序多,慢*/ public void bubblesort(int[] data) { for (int i = 1; i < data.length; i++) { boolean exchange = false; for (int j = 0; j < data.length - i; j++) { if (data[j] > data[j + 1]) { // 交换 int temp = data[j]; data[j] = data[j + 1]; data[j + 1] = temp; exchange = true; } } if (!exchange) break; } } /* 希尔排序 * 分组插入排序, * 增量序列一般为:5,3,1, * 取增量原则:最后必须是1,避免互为倍数*/ public void shellSort(int[] data, int[] spans) { for (int i = 0; i < spans.length; i++) { // 增量序列循环 int span = spans[i]; for (int j = 0; j < span; j++) {// 组间循环 for (int k = j + span; k < data.length; k = k + span) { // 组内循环 int temp = data[k]; int pre = k - span; while (pre >= 0 && data[pre] > temp) { data[pre + span] = data[pre]; pre = pre - span; }// end while data[pre + span] = temp; }// end for } }// end for }// end shellSort /* 直接插入排序 * 从第二个数开始, * 一个一个地插入到合适的位置*/ public void insertSort(int[] data) { for (int i = 1; i < data.length; i++) { int tmp = data[i]; if (tmp < data[i - 1]) { int j = i - 1; for (; j >= 0 && data[j] > tmp; j--) { data[j + 1] = data[j]; } data[j + 1] = tmp; } } } /* 直接选择排序 * 从当前无序区中选出最小的数据元素, * 将它与无序区的第一个元素互换*/ public void selectSort(int[] data) {// 直接选择排序的方法 for (int i = 0; i < data.length; i++) { int k = i; for (int j = i + 1; j < data.length; j++) { if (data[k] > (data[j])) { k = j; // 最小的数据元素 } } if (k != i) { int temp = data[i]; data[i] = data[k]; data[k] = temp; } } } /* 堆排序 * 将数组调准为大根堆, * 堆顶与最后项交换, * 无序区重新调整为堆, * 交换堆顶, * 重复步骤*/ public void heapSort(int[] data) { buildHeap(data); for (int i = data.length - 1; i > 0; i--) { int temp = data[0]; data[0] = data[i]; data[i] = temp; heapify(data, 0, i - 1); } } private void buildHeap(int[] data) { for (int i = data.length / 2; i >= 0; i--) { heapify(data, i, data.length - 1); } } private void heapify(int[] data, int low, int high) { int temp = data[low]; for (int large = 2 * low + 1; large <= high; large *= 2) { if (large < high && data[large] < data[large + 1]) { large++; } if (temp >= data[large]) { break; } data[low] = data[large]; low = large; } data[low] = temp; } /* 归并排序 * 递归分解成单元素的有序子序列, * 然后再把有序子序列合并为整体有序序列*/ public void mergeSort(int[] data, int left, int right) { if (left < right) { int center = (left + right) / 2; // 找出中间索引 mergeSort(data, left, center); // 对左边数组进行递归 mergeSort(data, center + 1, right); // 对右边数组进行递归 merge(data, left, center, right); // 合并 } } private void merge(int[] data, int left, int center, int right) { int[] tmpArr = new int[data.length]; int mid = center + 1; int index = left; // index记录中间数组的索引 int tmp = left; while (left <= center && mid <= right) { // 从两个数组中取出最小的放入中间数组 tmpArr[index++] = (data[left] <= data[mid]) ? data[left++] : data[mid++]; } while (mid <= right) { // 剩余部分依次放入中间数组 tmpArr[index++] = data[mid++]; } while (left <= center) { tmpArr[index++] = data[left++]; } while (tmp <= right) { data[tmp] = tmpArr[tmp++]; } } /* 基数排序 * 按最低位排序开始一直到按最高位排序, * 依次对各项进行排序, * 最终得到有序序列*/ public void radixSort(int[] data, int d) {//d=10^(n-1),n为最大数值的位数 int k = 0; int n = 1; int[][] temp = new int[data.length][data.length]; int[] order = new int[data.length]; while (n <= d) { for (int i = 0; i < data.length; i++) { int lsd = ((data[i] / n) % 10); temp[lsd][order[lsd]] = data[i]; order[lsd]++; } for (int i = 0; i < data.length; i++) { if (order[i] != 0) { for (int j = 0; j < order[i]; j++) { data[k] = temp[i][j]; k++; } order[i] = 0; //归零 } } n *= 10; //位数增加 k = 0; //归零 } } }