1. 程式人生 > >Java實現快速排序、歸併排序、堆排序和希爾排序

Java實現快速排序、歸併排序、堆排序和希爾排序

快速排序

演算法思想

1.將陣列的第一個元素取為target,定義兩個指標i 和 j;
2.指標i ,從左向右找到第一個比target大的元素,指標j從右向左找到第一個比target小的元素,若i小於j,則交換i, j 指標指向的元素的值;
3.指標i繼續從左向右搜尋,指標j繼續從右向左搜尋,重複步驟2直到某一次停下來以後i>j,此時交換target與j指向元素的位置,這樣即可保證target左邊的元素都小於它,target右邊的元素都大於它;
4.遞迴target左右兩部分,直到只剩下1個元素。
平均、最好時間複雜度:O(nlgn) 最差時間複雜度:O(n^2)
空間複雜度:O(1) (也可以說需要的空間是O(n),因為在遞迴呼叫時有棧的開銷,最壞是O(n),平均是O(logn))

程式碼實現

public class QuickSort {
    public static void quickSort(int[] R, int low, int high){
        if(low >= high)
            return;
        int i = low+1;
        int j = high;
        while(i<j){
            while(R[i]<=R[low] && i<high)
                i++;
            while
(R[j]>R[low] && j>low) j--; if(i<j){ int temp = R[i]; R[i] = R[j]; R[j] = temp; } } int temp = R[j]; R[j] = R[low]; R[low] = temp; quickSort(R, low, j - 1
); quickSort(R , j+1, high); } public static void main(String[] args) { int[] arr = new int[10000]; Random r = new Random(); for (int i = 0; i < arr.length; i++) { arr[i] = Math.abs(r.nextInt(1000)); } quickSort(arr, 0, arr.length - 1); if (check(arr)) System.out.println("right"); else System.out.println("wrong"); } public static boolean check(int[] arr) { for (int i = 1; i < arr.length; i++) { if (arr[i] < arr[i - 1]) { System.out.println(arr[i - 1] + " " + arr[i]); System.out.println("position is " + i); return false; } } return true; } }

歸併排序

演算法思想

1.將原陣列不斷二分,直到葉節點只剩一個元素
2.將同屬一個父節點的兩個兒子合併成有序的陣列,該父節點即變為有序陣列
3.遞迴的劃分併合並,即可得到最終有序的陣列
平均、最好、最差時間複雜度:O(nlgn) 空間複雜度:O(n)

public class mergeSort {
    public static void merge(int[] R, int low1, int high1, int low2, int high2){
        int i = low1;
        int j = low2;
        int[] temp = new int[high1-low1+1+high2-low2+1];
        int index = 0;
        while(i<=high1 && j<=high2){
            if(R[i]<=R[j]){
                temp[index] = R[i];
                i ++;
            }
            else{
                temp[index] = R[j];
                j ++;
            }
            index ++;
        }

        if(i>high1){
            while(j<=high2){
                temp[index] = R[j];
                j ++;
                index ++;
            }
        }

        if(j>high2){
            while(i<=high1){
                temp[index] = R[i];
                i ++;
                index ++;
            }
        }

        int m = low1;
        for(int k=0;k<temp.length;k++,m++)
            R[m] = temp[k];
    }

    public static void MergeSort(int[] R, int low, int high){
        if(low >= high)
            return;
        int mid = (low+high)/2;
        MergeSort(R, low, mid);
        MergeSort(R, mid+1, high);
        merge(R, low, mid, mid+1, high);
    }

    public static void main(String[] args){
        int[] R = {5,7,3,19,4,9,17,15,12,31,23,30,5,8,1,17,16,5,3};
        MergeSort(R, 0, R.length-1);
        for(int i=0;i<R.length;i++)
            System.out.printf("%d ",R[i]);
    }
}

堆排序

public class HeapSort {

    public static void rebuildHeap(int[] A, int start, int end){
        int i = start;
        while(i<=end/2 -1) {
            if(A[i]>A[2*i+1] && 2*i+2<end && A[i]>A[2*i+2]|| A[i]>A[2*i+1] && 2*i+2>end )
                break;
            else{
                if(2*i+2<end) {
                    if (A[2*i + 1] >= A[2*i + 2]) {
                        swap(A, i, 2 * i + 1);
                        i = 2*i + 1;
                    }
                    else {
                        swap(A,i,2*i +2);
                        i = 2*i + 2;
                    }
                }
                else{
                    swap(A, i, 2 * i + 1);
                    i = 2*i + 1;
                }
            }
        }
    }

    public static void HeapSort(int[] A){
        int n = A.length;
        int start = n/2 - 1;
        for(int i=start;i>=0;i-- ){
            rebuildHeap(A, i, n-1);
        }
        for(int j=n-1;j>0;j--){
            swap(A, 0, j);
            rebuildHeap(A,0,j-1);
        }
    }
    public static void swap(int[] A,int a,int b){
        int temp = A[a];
        A[a] = A[b];
        A[b] = temp;
    }
    public static void main(String[] args){
        int[] A = new int[]{6,12,3,7,8,2,9,11,35,12,8,22};
        HeapSort(A);
        for(int i:A){
            System.out.println(i);
        }
    }
}

希爾排序

public class ShellSort {
    public static void shellSort(int[] A){
        for(int gap = A.length/2; gap > 0; gap/=2)
            for(int i=gap;i<A.length;i++){
                for(int j=i;j>=gap;j-=gap){
                    if(A[j]<A[j-gap]){
                        int temp = A[j];
                        A[j] = A[j-gap];
                        A[j-gap] = temp;
                    }
                    else break;
                }
            }
    }
    public static void main(String[] args){
        int[] A = new int[]{6,2,12,3,5,14,22,24,7};
        shellSort(A);
        for(int x:A)
            System.out.println(x);
    }
}