冒泡排序
思路:就是每次將最大或最小的元素放到數(shù)組的最后,so easy!時(shí)間復(fù)雜度為(O(n^2))
public class BubbleSort { public static void bubbleSort(int[] a) { for (int j = 1; j < a.length; j++) { for (int i = 0; i < a.length - j; i++) { public static void main(String[] args) { int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7, for(int i = 0; i < a.length; i++){ System.out.print(a[i]+" ");
選擇排序
思路:類似于冒泡,每次將后面最小的元素放在前面。時(shí)間復(fù)雜度為((O(n^2)))
public class SelectSort { public static void selectSort(int[] a) { for (int i = 0; i < a.length - 1; i++) { for (int j = min + 1; j < a.length; j++) { public static void main(String[] args) { int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7, 3, 8, 31 }; for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " ");
插入排序
思路:從第二個(gè)元素開(kāi)始,和之前的已排好序的字?jǐn)?shù)組的元素比較,找到合適的位置,然后后面的元素向后移動(dòng)一位,再將該元素插入到前面合適的位置。時(shí)間復(fù)雜度為(O(n^2))
public class InsertSort { public static void insertSort(int[] a) { for (int i = 1; i < a.length; i++) { while (j >= 0 && a[j] > key) { public static void main(String[] args) { int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7, 3, 8, 31 }; for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " ");
二分法排序
思路:插入排序的多此一舉,居然先用二分法查找插入位置(多此一舉),然后再所有插入位置(二分法查出來(lái)的)后面的元素全部后移,太蛋疼了,插入法直接邊找邊后移多容易啊,這個(gè)事蛋疼的做法,下午想了一下做出來(lái)。(14、08、3)
public class BinarySort { public static int binarySerch(int[] arr, int start, int end, int value) { else if (arr[mid] > value) else if (value < arr[mid]) public static void binarySort(int[] arr, int start, int end) { for (int i = start + 1; i <= end; i++) { int insertLoc = binarySerch(arr, start, i - 1, value) ; for (int j = i; j > insertLoc; j--) { public static void main(String[] args) { int[] arr = { 3, 5, 0, 8, 7, 9, 1, 2, 6, 4 }; binarySort(arr, 0, arr.length - 1); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " ");
希爾排序
思路:類似于插入排序,只是每次所取的步長(zhǎng)為(數(shù)組的長(zhǎng)度 / 2 / i)。時(shí)間復(fù)雜度為(n*log n)。
public static void shellSort(int[] a) { for (int gap = a.length / 2; gap > 0; gap /= 2) for (int i = gap; i < a.length; i++) { while (j >= 0 && a[j] > key) { public static void main(String[] args) { int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7, 3, 8, 31 }; for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " ");
快速排序
思路:關(guān)鍵在于求出partition()函數(shù)。我給出了兩種方法:1、從前到后。2、從前到中間,從后到中間。時(shí)間復(fù)雜度為(n * log n)最壞情況為
OK!show your my codes!
/*public static int partition(int[] a, int p, int r) { for (int j = p; j < r; j++) { if (a[j] <= x) {//如果a[j]<x就將后面a[i]后面a[i+1](值大于x)與a[j](值<a[j])進(jìn)行交換 public static int partition(int a[], int p, int r) { a[i] = a[j];//把小于X的那個(gè)數(shù)拿到前面的a【i】中 a[j] = a[i];//把大于X的那個(gè)數(shù)拿到后面的a【j】中 public static void quickSort(int[] a, int p, int r) { int q = partition(a, p, r); public static void main(String[] args) { int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7, quickSort(a, 0, a.length-1); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " ");
合并排序
思路:用分治的思想,將數(shù)組分至最小,再合并即可,不懂自己google吧!時(shí)間復(fù)雜度為(n * log n )是一個(gè)穩(wěn)定的排序算法。
public static void merge(int A[], int p, int q, int r) { int[] L = new int[q - p + 1]; int[] R = new int[r - q]; for (int i = p, j = 0; i <= q; i++, j++) { for (int i = q + 1, j = 0; i <= r; i++, j++) { for (; i < L.length && j < R.length;) { } else if (j < R.length) { public static void mergeSort(int[] A, int p, int r) { public static void main(String[] args) { int A[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7, mergeSort(A, 0, A.length - 1); for (int i = 0; i < A.length; i++) { System.out.print(A[i] + " ");
堆排序
思路:建立一個(gè)堆,大頂堆或者小頂堆都可以。每次將堆頂元素放到數(shù)組最后,然后堆的規(guī)模減小一個(gè),再維持第一個(gè)元素的大頂堆性質(zhì)。時(shí)間復(fù)雜度為(n * log n)。
static int parent(int i) { static int right(int i) { static void maxHelpify(int[] A, int i) { if (l <= A[0] && A[l] > A[i]) if (r <= A[0] && A[r] > A[largest]) static void buildMaxHeap(int[] A){ for(int i=(A.length-1)/2; i>0; i--){ public static void heapSort(int[] A){ //每次把堆頂?shù)臄?shù)放到數(shù)組最后,然后把堆的大小減1,再次維持第一個(gè)數(shù)的大頂堆性質(zhì) for(int i = A.length - 1; i>=2; i--){ public static void main(String[] args) { A[0] = A.length-1;//a[0]存放堆的大小 for(int i = 1; i < A.length; i++){ A[i] = (int) (Math.random()*10); for (int i = 1; i < A.length; i++) { System.out.print(A[i] + " ");
其他:還有一種計(jì)數(shù)排序。
比較簡(jiǎn)單:就是將數(shù)組下標(biāo)作為元素的value,特殊情況下使用。如排序N個(gè)人的年齡就可以用計(jì)數(shù)排序。將年齡看作數(shù)組的下標(biāo),定義一個(gè)大小為100的數(shù)組,將年齡與之比較,如果年齡==下標(biāo)就將,該下標(biāo)的value+1.時(shí)間復(fù)雜度為(N)。
|