首页 > 代码库 > 插入排序,希尔排序,堆排序,归并排序,快速排序Java实现
插入排序,希尔排序,堆排序,归并排序,快速排序Java实现
参看:数据结构与算法分析-c语言描述
public class Main { public static void main(String[] args) { String[] a = { "a", "d", "e", "f", "m" }; String[] b = { "a", "h", "g", "l", "z" }; String[] c = { "z", "d", "a", "h", "z", "a", "d", "e", "f", "m" };// quickSort(c, 0, 9); for (int i = 0; i < c.length; i++) { System.out.println(c[i]); } } public static int[] testttt() { return new int[10]; } public static <T extends Comparable<T>> void insertSort(T[] a) { if (a.length < 0) return; for (int i = 1; i < a.length; i++) { T temp = a[i]; int j = i - 1; for (; j >= 0 && a[j].compareTo(a[i]) > 0; j--) { a[j + 1] = a[j]; } a[j + 1] = temp; } } public static <T extends Comparable<T>> void shellSort(T[] a) { if (a.length < 0) return; for (int gap = a.length / 2; gap > 0; gap /= 2) { for (int i = gap; i < a.length; i++) { int j = gap; T temp = a[j]; for (; j > 0 && temp.compareTo(a[j - gap]) < 0; j -= gap) { a[j] = a[j - gap]; } a[j] = temp; } } } private static int leftChild(int i) { return 2 * i + 1; } public static <T extends Comparable<T>> void perChange(T[] array, int i, int n) {// 从i位置开始,到n,进行交换找到最大元素 int child; T temp = array[i]; for (; leftChild(i) < n; i = child) { child = leftChild(i); if (child != n - 1 && array[child].compareTo(array[child + 1]) < 0) child++; if (temp.compareTo(array[child]) < 0) array[i] = array[child]; else break; } array[i] = temp; } public static <T extends Comparable<T>> void heapSort(T[] array) { for (int i = array.length / 2; i > 0; i--) { perChange(array, i, array.length); } for (int i = array.length - 1; i > 0; i--) { T temp = array[0]; array[0] = array[i]; array[i] = temp; perChange(array, 0, i); } } public static String[] merge(String[] a, String[] b) { int aLength = a.length; int bLength = b.length; String[] temp = new String[aLength + bLength]; int i, j, k; for (i = 0, j = 0, k = 0; i < aLength && j < bLength;) { if (a[i].compareTo(b[j]) > 0) { temp[k] = b[j]; j++; k++; } else { temp[k] = a[i]; i++; k++; } } if (i == aLength) { for (; j < bLength; j++, k++) { temp[k] = b[j]; } } else { for (; i < aLength; i++, k++) { temp[k] = a[i]; } } return temp; } public static void mergeSort(String[] a, int left, int right) { if (left < right) { int middle = (left + right) / 2; mergeSort(a, left, middle); mergeSort(a, middle + 1, right); merge(a, left, right, middle); } } public static void merge(String[] a, int left, int right, int middle) { String[] temp = new String[a.length]; int i, j, k; for (i = left, j = middle + 1, k = 0; i <= middle && j <= right;) { if (a[i].compareTo(a[j]) > 0) { temp[k] = a[j]; j++; k++; } else { temp[k] = a[i]; i++; k++; } } if (i == middle + 1) { for (; j <= right; j++, k++) { temp[k] = a[j]; } } else { for (; i <= middle; i++, k++) { temp[k] = a[i]; } } for (int i1 = left, k2 = 0; i1 <= right; i1++, k2++) { a[i1] = temp[k2]; } } public static void quickSort(String[] a, int left, int right) { if (left < right) { int p = partion(a, left, right); quickSort(a, left, p - 1); quickSort(a, p + 1, right); } } private static int partion(String[] a, int left, int right) { String temp = a[left]; while (left < right) { while (left < right && a[right].compareTo(temp) >=0){ right--; } a[left] = a[right]; while (left < right && a[left].compareTo(temp) <=0){ left++; } a[right] = a[left]; } a[left] = temp; return left; }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。