Quick sort and heap sort

Quick Sort idea: in partition, first to the rightmost value as the division value x, maintenance intervals are less than x, x is equal to the interval, as well as three intervals greater than x, and returns left and right boundaries divide value. time complexity is O (nlogn).


public class QuickSort {
    public static void quickSort(int[] arr) {
        if(arr == null || arr.length < 2)
            return ;
        sortProgress(arr, 0 , arr.length - 1);
    }
    public static void sortProgress(int[] arr, int L, int R) {
        if(L < R) {
            //随机取L到R之间的一个数与R交换.
            swap(arr, L + (int)(Math.random() * (R - L + 1)), R);
            //p数组的大小为2,p[0]表示划分值的左边界,p[1]表示划分值的右边界.
            int[] p = partition(arr, L, R);
            sortProgress(arr, L, p[0] - 1);
            sortProgress(arr, p[1] + 1, R);
        }
    }
    public static int[] partition(int[] arr, int L, int R) {
        int less = L - 1;
        int more = R;
        while(L < more) {
            if(arr[L] < arr[R]) {
                swap(arr, ++less, L++);
            }else if(arr[L] > arr[R]) {
                swap(arr, --more, L);
            }else {
                L++;
            }
        }
        swap(arr, more, R);
        //返回划分值的左边界和右边界
        return new int[] {less + 1, more};
    }
    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    } 
    public static void main(String[] args) {
        int[] arr = new int[]{123,45,767,343,654,2,66,88};
        System.out.print("原始数组:");
        for(int i = 0; i < arr.length; i++)
            if(i != arr.length - 1)
                System.out.print(arr[i] + " ");
            else
                System.out.println(arr[i]);
        quickSort(arr);
        System.out.print("快速排序后:");
        for(int i = 0; i < arr.length; i++)
            if(i != arr.length)
                System.out.print(arr[i] + " ");
            else
                System.out.println(arr[i]);
    }   
}

operation result:
Quick sort and heap sort

HEAPSORT thought: the largest in the process of constructing the piles, each adjusted upward, and only parent nodes are compared, the stack build time complexity is O (n), in the process of heap sort, the elements take the top of the stack and the last a switching element, then the top of the stack downward adjustment elements, maintain the maximum stack, to complete the final sorting. time complexity of O (nlogn).


public class HeapSort {
public static void heapSort(int[] arr) {
    if(arr == null || arr.length < 2)
        return ;
    for(int i = 0; i < arr.length; i++)
    heapInsert(arr, i);
    int size = arr.length;
    swap(arr, 0, --size);
    while(size > 0) {
        heapify(arr, 0, size);
        swap(arr, 0, --size);
    }

}
public static void heapInsert(int[] arr, int index){
    while(arr[index] > arr[(index - 1)/2]) {
        swap(arr, index, (index - 1)/2);
        index = (index - 1) / 2;
    }
}
public static void heapify(int[] arr, int index, int size){
    int left = 2 * index + 1;
    while(left < size){
        int largest = left + 1 < size && arr[left + 1] > arr[left]? left + 1: left;
        largest = arr[index] > arr[largest] ? index: largest;
        if(largest == index) {
            break;
        }
        swap(arr, index, largest);
        index = largest;
        left = 2 * index + 1;
    }
}
public static void swap(int[] arr, int i, int j) {
    int tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
}
    public static void main(String[] args) {
        int[] arr = new int[] {3,66,65,43,213,76,66,23};
        System.out.print("原始数组:");
        for(int i = 0; i < arr.length; i++)
            if(i != arr.length - 1)
            System.out.print(arr[i] + " ");
            else
                System.out.println(arr[i]);
        heapSort(arr);
        System.out.print("堆排序后:");
        for(int i = 0; i < arr.length; i++)
            if(i != arr.length - 1)
                System.out.print(arr[i] + " ");
            else
                System.out.println(arr[i]);
    }
}

The result:
Quick sort and heap sort
summary of
quick sort is unstable. Algorithm time complexity of O (nlogn)
heap sort is unstable. Time complexity of O (nlogn).

Guess you like

Origin blog.51cto.com/14472348/2480239