转发请注明原创地址 http://www.cnblogs.com/dongxiao-yang/p/6264831.html
冒泡
public static int[] bubble_sort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) {//每次循环代表一轮冒泡,一轮冒泡后顶端的数字一定是最大的 for (int j = 0; j < n - 1 - i; j++) { if (arr[j] < (arr[j + 1])) { int tmp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = tmp; } } } return arr; }
快速排序1
public static void quick_sort(int[] arr, int start, int end) { if (start >= end) { // System.out.println("qs finish"); return; } int x = arr[start]; int s = start; int e = end; while (s < e) { if (arr[e] < x) { for (; s < e; s++) { if (arr[s] > x) { int tmp = arr[s]; arr[s] = arr[e]; arr[e] = tmp; // System.out.println("switch " + arr[s] + " " + // arr[e]); // System.out.println("s is " + s + " e is " + e); break; } } } else { e--; } } // System.out.println(" the end s is " + s + " e is " + e + // " arr[s] is " // + arr[s]+" arr[start] is "+arr[start]); arr[start] = arr[s]; arr[s] = x; System.out.println("quick_sort is "+Arrays.toString(arr)); quick_sort(arr, start, s - 1); quick_sort(arr, s + 1, end); }
快速排序2代码优化
public static void quick_sort2(int[] arr, int start, int end) { if (start < end) { int x = arr[start]; int s = start; int e = end; while (s < e) { //退出循环代表一轮排序结束,首尾游标相遇 while (s < e && (arr[e] >= x)) // 从右向左找第一个小于x的数 { e--; } while (s < e && (arr[s] <= x)) // 从右向左找第一个小于x的数 { s++; } if (s < e) {// 左右两个数交换 int tmp = arr[s]; arr[s] = arr[e]; arr[e] = tmp; // System.out.println("switch " + arr[s] + " " + arr[e]); // System.out.println("s is " + s + " e is " + e); } } arr[start] = arr[s]; //基准数归位到数组中正确位置 arr[s] = x; System.out.println("quick_sort2 is "+Arrays.toString(arr)); quick_sort2(arr, start, s - 1); quick_sort2(arr, s + 1, end); } }