标签:style blog color os java ar for art div
个人总结的常见的排序算法
public class Sort { // 1、冒泡:稳定,最优O(n) 最差O(n^2) 平均O(n^2) private static void sort1(int[] arr) { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length - 1; j++) { if (arr[j] > arr[j + 1]) { int tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } } // 2、选择:不稳定,最优O(n^2) 最差O(n^2) 平均O(n^2) private static void sort2(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = i + 1; j < arr.length; j++) { if (arr[i] > arr[j]) { int tmp = arr[j]; arr[j] = arr[i]; arr[i] = tmp; } } } } // 3、插入:稳定,最优O(n) 最差O(n^2) 平均O(n^2) private static void sort3(int[] arr) { int insertData = 0; for (int i = 0; i < arr.length; i++) { insertData = arr[i]; int j = i - 1; while (j >= 0 && insertData < arr[j]) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = insertData; } } // 4、希尔:不稳定,最优O(n) 最差O(n^1.5) 平均O(n^1.5) private static void sort4(int[] arr) { int space = arr.length; while (space > 1) { space = space / 2; for (int i = 0; i < space; i++) { for (int j = i + space; j < arr.length; j = j + space) { int insertData = arr[j]; int k = j - space; while (k >= i && insertData < arr[k]) { arr[k + space] = arr[k]; k = k - space; } arr[k + space] = insertData; } } } } // 5、快速:不稳定,最优O(nlogn) 最差O(n^2) 平均O(nlogn) private static void sort5(int[] arr, int start, int end) { int left = start; int right = end - 1; int pivot = arr[end]; while (left < right) { if (arr[left] < pivot) { left++; continue; } if (arr[right] > pivot) { right--; continue; } swap(arr, left, right); left++; right--; } if (arr[left] < pivot) { left++; } swap(arr, left, end); if (left - 1 > start) sort5(arr, start, left - 1); if (left + 1 < end) sort5(arr, left + 1, end); } private static void swap(int[] arr, int x, int y) { int tmp = arr[x]; arr[x] = arr[y]; arr[y] = tmp; } // 6、归并:稳定,最优O(nlogn) 最差O(nlogn) 平均O(nlogn) public static void sort6(int[] arr, int low, int high) { int mid = (low + high) / 2; if (low < high) { sort6(arr, low, mid); sort6(arr, mid + 1, high); merge(arr, low, mid, high); } } public static void merge(int[] arr, int low, int mid, int high) { int[] temp = new int[high - low + 1]; int i = low;// 左指针 int j = mid + 1;// 右指针 int k = 0; // 把较小的数先移到新数组中 while (i <= mid && j <= high) { if (arr[i] < arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; } } // 把左边剩余的数移入数组 while (i <= mid) { temp[k++] = arr[i++]; } // 把右边边剩余的数移入数组 while (j <= high) { temp[k++] = arr[j++]; } // 把新数组中的数覆盖nums数组 for (int k2 = 0; k2 < temp.length; k2++) { arr[k2 + low] = temp[k2]; } } // 7、堆:不稳定,最优O(nlogn) 最差O(nlogn) 平均O(nlogn) private static void sort7(int[] arr) { } // 测试 public static void main(String[] args) { int[] array = { 10, 8, 24, 32, 7, 6, 100, 55, 43, 21, 56, 55, 10000, 3000 }; // sort1(array); // sort2(array); // sort3(array); // sort4(array); // sort5(array,0,array.length-1); sort6(array, 0, array.length - 1); System.out.println(Arrays.toString(array)); } }
标签:style blog color os java ar for art div
原文地址:http://www.cnblogs.com/suijie/p/3969866.html