java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java 排序算法

Java综合整理堆排序 快速排序 归并排序

作者:小写丶H

堆排序是利用堆这种数据结构而设计的一种排序算法,堆排序是一种选择排序,它的最坏,最好,平均时间复杂度均为O(nlogn),它也是不稳定排序。首先简单了解下堆结构

堆排序

private static void heapSort(int[] arr) {
		//建堆
        crearHeap(arr);
        for (int i = 0; i < arr.length-1; i++) {
            int heapSize=arr.length-i;
            swap(arr,heapSize-1,0);
            heapSize--;
            shiftDown(arr,heapSize,0);
        }
        System.out.println(Arrays.toString(arr));
    }

    private static void crearHeap(int[] arr) {
    	// 从后往前遍历(右边非叶子节点开始), 依次进行向下调整
        for (int i = (arr.length-1-1)/2; i >=0 ; i--) {
            shiftDown(arr,arr.length,i);
        }
        
    }
	//向下调整,形成大堆
    private static void shiftDown(int[] arr, int size, int i) {
        int parent = i;
        int child = parent*2+1;
        while (child<size){
            if (child +1< size && arr[child +1]> arr[child]){
                child=child+1;
            }
            if (arr[child]>arr[parent]){
                swap(arr,child,parent);
            }else {
                break;
            }
            parent=child;
            child=parent*2+1;
        }
    }
	//交换
    private static void swap(int[] arr, int child, int parent) {
        int tmp =arr[child];
        arr[child] =arr[parent];
        arr[parent]=tmp;
    }

快速排序

递归

private static void quick(int[] arr) {
        quickSortHelper(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
    }

    private static void quickSortHelper(int[] arr, int left, int right) {
        if (left>=right){
            //区间只有一个元素,或者零个元素
            return;
        }
        int index = partition(arr,left,right);
        quickSortHelper(arr,left,index-1);
        quickSortHelper(arr,index+1,right);
    }

    private static int partition(int[] arr, int left, int right) {
        int i=left;
        int j=right;
        int baseValue=arr[right];
        while (i<j){
            while (i<j && arr[i]<=baseValue){
                i++;
            }
            while (i<j && arr[j]>=baseValue){
                j--;
            }
            if (i<j){
                swap(arr,i,j);
            }
        }
        swap(arr,i,right);
        return i;
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp =arr[i];
        arr[i]=arr[j];
        arr[j]=tmp;
    }

非递归

public static void quickSortByLoop(int[] arr) {
        Stack<Integer> stack =new Stack<>();
        stack.push(0);
        stack.push(arr.length-1);
        while (!stack.isEmpty()){
            int right = stack.pop();
            int left = stack.pop();
            if (left>=right){
                continue;
            }
            int index = partition(arr,left,right);
            //右子树
            stack.push(index+1);
            stack.push(right);
            //左子树
            stack.push(left);
            stack.push(index-1);
        }
        System.out.println(Arrays.toString(arr));
    }

    private static int partition(int[] arr, int left, int right) {
        int baseValue =arr[right];
        int i =left;
        int j =right;
        while (i<j){
            while (i<j && arr[i]<=baseValue){
                i++;
            }
            while (i<j && arr[j]>=baseValue){
                j--;
            }
            if (i<j){
                swap(arr,i,j);
            }
        }
        swap(arr,i,right);
        return i;
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

归并排序

递归

public static void mergeSort(int[] arr){
        mergeSortHelper(arr,0,arr.length);
        System.out.println(Arrays.toString(arr));
    }

    private static void mergeSortHelper(int[] arr, int left, int right) {
        if (right-left<=1){
            return;
        }
        int mid = (right+left)/2;

        mergeSortHelper(arr,left,mid);
        mergeSortHelper(arr,mid,right);
        merge(arr,left,mid,right);
    }

    private static void merge(int[] arr, int left, int mid, int right) {
        int cur1 =left;
        int cur2 =mid;
        //两个数组合并后的结果
        int[] output=new int[right-left];
        int outputIndex=0;

        while (cur1<mid && cur2<right){
            if (arr[cur1]<=arr[cur2]) {
                output[outputIndex++] = arr[cur1++];
            }else {
                output[outputIndex++] = arr[cur2++];
            }
        }
        while (cur1<mid){
            output[outputIndex++] = arr[cur1++];
        }
        while (cur2<right){
            output[outputIndex++] = arr[cur2++];
        }

        for (int i = 0; i < right-left ; i++) {
            arr[left+i] = output[i];
        }
    }

非递归

public static void mergeSortByLoop(int[] arr){
        // gap 当前每个组中的元素个数.
        for (int gap =1;gap<arr.length;gap*=2){
            for (int i = 0; i <arr.length ; i+=2*gap) {
                //相当于把两个长度为 gap 的相邻组进行了合并
                int left =i;
                int mid =i+gap;
                int right=i+2*gap;
                if (mid > arr.length){
                    mid =arr.length;
                }
                if (right>arr.length){
                    right=arr.length;
                }
                merge(arr,left,mid,right);
            }
        }
        System.out.println(Arrays.toString(arr));
    }

到此这篇关于Java综合整理堆排序 快速排序 归并排序的文章就介绍到这了,更多相关Java 排序算法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文