三大排序算法

merge sort

空间复杂度:O(n)(sort list是O(1))

时间复杂度为O(nlgn)

稳定性:稳定排序

先局部有序,再整体有序

 1 public class Solution {
 2     /**
 3      * @param A an integer array
 4      * @return void
 5      */
 6     public void sortIntegers(int[] A) {
 7         int[] temp = new int[A.length];
 8         mergeSort(A, 0, A.length - 1, temp);
 9     }
10     
11     public void mergeSort(int[] A, int start, int end, int[] temp) {
12         if (start >= end) {
13             return;
14         }
15         mergeSort(A, start, (start + end) / 2, temp);
16         mergeSort(A, (start + end) / 2 + 1, end, temp);
17         
18         merge(A, start, end, temp);
19     }
20     
21     public void merge(int[] A, int start, int end, int[] temp) {
22         int mid = (start + end) / 2;
23         int left = start;
24         int right = mid + 1;
25         int index = start;
26         
27         while (left <= mid && right <= end) {
28             if (A[left] <= A[right]) {
29                 temp[index++] = A[left++];
30             } else {
31                 temp[index++] = A[right++];
32             }
33         }
34         while (left <= mid) {
35             temp[index++] = A[left++];
36         }
37         while (right <= end) {
38             temp[index++] = A[right++];
39         }
40         for (int i = start; i <= end; i++) {
41             A[i] = temp[i];
42         }
43     }
44 }
View Code

quick sort

空间复杂度:O(1)的额外空间

时间复杂度:期望时间复杂度为O(nlgn),最坏情况下时间复杂度为O(n^2)

稳定性:不稳定排序

先整体有序,再局部有序

 1 public class Solution {
 2     /**
 3      * @param A an integer array
 4      * @return void
 5      */
 6     public void sortIntegers(int[] A) {
 7         if (A == null || A.length == 0) {
 8             return;
 9         }
10         quickSort(A, 0, A.length - 1);
11     }
12     
13     public void quickSort(int[] A, int start, int end) {
14         if (start >= end) {
15             return;
16         }
17         int left = start;
18         int right = end;
19         int pivot = A[(start + end) / 2];
20         
21         while (left <= right) {
22             while (left <= right && A[left] < pivot) {
23                 left++;
24             }
25             while (left <= right && A[right] > pivot) {
26                 right--;
27             }
28             if (left <= right) {
29                 int tmp = A[left];
30                 A[left] = A[right];
31                 A[right] = tmp;
32                 left++;
33                 right--;
34             }
35         }
36         
37         quickSort(A, start, right);
38         quickSort(A, left, end);
39     }
40 }
View Code

heap sort

空间复杂度:O(1)

时间复杂度: O(nlgn)

步骤:把数组A建立成一个大根堆(从左到右shiftup操作)。然后把堆顶元素(即最大值)与堆末尾元素交换,再shiftdown,重复n次。

public class Solution {
    /**
     * @param A: an integer array
     * @return: nothing
     */
    public void sortIntegers2(int[] A) {
        if (A == null) {
            return;
        }
        buildHeap(A);
        int size = A.length;
        for (int i = A.length - 1; i >= 0; i--) {
            swap(A, 0, i);
            size--;
            shiftDown(A, 0, size);
        }
    }
    
    public void buildHeap(int[] A) {
        for (int i = 0; i < A.length; i++) {
            shiftUp(A, i);
        }
    }
    
    public void shiftUp(int[] A, int cur) {
        if (cur == 0) {
            return;
        }
        int father = cur % 2 == 0 ? cur / 2 - 1 : cur / 2;
        if (A[father] < A[cur]) {
            swap(A, father, cur);
        }
        shiftUp(A, father);
    }
    
    public void shiftDown(int[] A, int cur, int size) {
        if (cur >= size - 1) {
            return;
        }
        int left = cur * 2 + 1;
        int right = cur * 2 + 2;
        
        int max_child = -1;
        if (left <= size - 1 && right <= size - 1) {
            max_child = A[left] < A[right] ? right : left;
        } else if (left <= size - 1) {
            max_child = left;
        } else {
            return;
        }
        if(A[cur] < A[max_child]) {
            swap(A, cur, max_child);
            shiftDown(A, max_child, size);
        }
    }
    
    public void swap(int[] A, int i, int j) {
        int tmp = A[i];
        A[i] = A[j];
        A[j] = tmp;
    }
}
View Code
原文地址:https://www.cnblogs.com/coldyan/p/6083453.html