排序小结自用

插入排序:

 /**
     * 时间复杂度:
     * 最坏情况下:O(n^2)【逆序的时候】
     * 最好情况下:O(n)【越有序越快】
     * 空间复杂度:O(1)
     *
     * 稳定性:稳定的排序
     * 【一个稳定的排序,可以实现为不稳定的排序。
     *  但是本身就是一个不稳定的排序,你是不可能实现为一个稳定的排序的】
     *
     * 技巧:如何快速判断一个排序是否稳定? 就看是否有发生跳跃式的交换
     *
     * 场景:同学,我有一组数据,数量不太多,可以认为是常数,而且,这些数据已近大部分有序了或者趋近于有序了。
     * :O(n)
     * @param array
     */
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= 0; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    //array[j+1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

希尔排序:

/**
     * 时间复杂度:O(n^1.3 - n^1.5)
     * 空间复杂度:O(1)
     * 稳定性:不稳定的排序
     * @param array
     */

    public static void shellSort1(int[] array) {
        int[] drr = {5,3,1};//增量序列,这个不好求。 有1W个数据 也是5 3 1 吗?? 肯定不是
        for (int i = 0; i < drr.length; i++) {
            shell(array,drr[i]);
        }
    }

    public static void shell(int[] array,int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            for (; j >= 0; j -= gap) {
                if(array[j] > tmp) {
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

选择排序:

 /**
     * 时间复杂度:O(n^2)
     * 空间复杂度:O(1)
     * 稳定性:不稳定的排序
     * @param array
     */
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = i+1; j < array.length; j++) {
                if(array[i] > array[j]) {
                    int tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                }
            }
        }
    }

堆排序:

public static void shiftDown(int[] array,int parent,int len) {
        int child = (2*parent)+1;
        while (child < len) {
            if(child+1 < len && array[child] < array[child+1]) {
                child++;
            }
            if(array[child] > array[parent]) {
                int tmp = array[child];
                array[child] = array[parent];
                array[parent] = tmp;
                parent = child;
                child = 2*parent+1;
            }else {
                break;
            }
        }
    }

    public static void createHeap(int[] array) {
        for (int i = (array.length-1-1)/2; i >= 0 ; i--) {
            shiftDown(array,i,array.length);
        }
    }

    /**
     * 时间复杂度:O(N*logN)
     * 空间复杂度:O(1)
     * 稳定性:不稳定
     * @param array
     */
    public static void heapSort(int[] array) {
        //大根堆 O(N)
        createHeap(array);

        int end = array.length-1;
        while (end > 0) {
            int tmp = array[end];
            array[end] = array[0];
            array[0] = tmp;
            shiftDown(array,0,end);
            end--;
        }
    }

冒泡排序:

/**
     * 都是不优化冒泡情况下:
     * 时间复杂度:O(n^2)
     * 空间复杂度:O(1)
     * 稳定性:稳定的排序
     * @param array
     */
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j] > array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flg = true;
                }
            }
            if(!flg) {
                break;
            }
        }
    }

快速排序:

public static int partition(int[] array,int start,int end) {
        int tmp = array[start];
        while (start < end) {
            while (start < end && array[end] >= tmp) {
                end--;
            }
            array[start] = array[end];
            while (start < end && array[start] <= tmp) {
                start++;
            }
            array[end] = array[start];
        }
        array[start] = tmp;
        return start;
    }
public static void quick(int[] array,int low,int high) {
        if(low >= high) {
            return;
}
        int pivot = partition(array,low,high);
        quick(array,low,pivot-1);
        quick(array,pivot+1,high);
}
/**
     * 时间复杂度:O(N*logn)
     * 最好 :O(N*logn). 每次能够均匀的分割待排序序列
     * 最坏:O(N^2)   有序
     * 空间复杂度:
     * 最好 :logn
     * 最坏:O(n)
     * 稳定性:不稳定的排序
     * @param array
     */
//非递归分治
    public static void quickSort(int[] array){
    Stack<Integer> stack = new Stack<>();
    int low = 0;
    int high = array.length-1;
    int privo = partition(array,low,high);
    if(privo > low+1){
        stack.push(low);
        stack.push(privo-1);
    }if (privo < high-1){
        stack.push(privo+1);
        stack.push(high);
        }
    while(!stack.empty()){
        high = stack.pop();
        low = stack.pop();
        privo = partition(array,low,high);
        if(privo > low+1){
            stack.push(low);
            stack.push(privo-1);
        }if (privo < high-1){
            stack.push(privo+1);
            stack.push(high);
        }
    }
    }

归并排序:

 public static void merge(int[] array,int low,int mid,int high) {
        int s1 = low;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = high;
        int[] tmpArr = new int[high-low+1];
        int k = 0;
        while (s1 <= e1 && s2 <= e2) {
            //不加等于号  就不是稳定的排序
            if(array[s1] <= array[s2]) {
                tmpArr[k++] = array[s1++];
            }else {
                tmpArr[k++] = array[s2++];
            }
        }
        while (s1 <= e1) {
            tmpArr[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmpArr[k++] = array[s2++];
        }
        //写回数据到原来的数组
        for (int i = 0; i < k; i++) {
            array[i+low] = tmpArr[i];
        }
    }

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

//非递归
    public static void mergeSortNor(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i = i+gap*2) {
                int low = i;
                int mid = low+gap-1;
                int high = mid+gap;
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                if(high >= array.length) {
                    high = array.length-1;
                }
                merge(array,low,mid,high);
            }
            gap = gap*2;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值