常见的排序

1、直接插入排序

/*  直接插入排序
 * 时间复杂度:
 * 最坏情况 O(n^2) 【逆序】
 * 最好情况 O(n)   【越有序越好】
 * 空间复杂度:O(1)
 * 稳定性:稳定的排序(相同数字排序前后位置是否发生改变)
 * 【一个稳定的排序可以实现为不稳定的排序。
 * 但是本身不稳定的排序,不可能实现为稳定排序】
 *
 * 如何快速判断一个排序是否稳定? 就看是否发生跳跃式的交换
 * */
public static void insertSort(int[] array){
        for (int i = 1; i < array.length; i++) {
            int j = i-1;
            int tmp = array[i];
            for (; j >= 0 ; j--) {
                if(array[j] > tmp){
                    array[j+1] = array[j];
                }else{
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

2、希尔排序(特殊的直接插入排序)

*  希尔排序(分组思想)
 * 时间复杂度:O(n^1.3) ~ O(n^1.5)
 * 空间复杂度:O(1)
 * 稳定性:不稳定的排序(相同数字排序前后位置是否发生改变)
 *
 * */
public static void shellSort(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;
        }
    }
    public static void getGap(int[] array){
        int[] gap = {5,3,1};
        for (int i = 0; i < gap.length; i++) {
            shellSort(array,gap[i]);
        }
    }

3、选择排序

/*  选择排序
 * 时间复杂度:
 * 最坏情况 O(n^2)
 * 空间复杂度:O(1)
 * 稳定性:不稳定的排序(相同数字排序前后位置是否发生改变)
 *
 * */
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[j] < array[i]){
                    int tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                }
            }
        }
    }

4、堆排序(大根堆)

/*堆排序
 *时间复杂度:O(n*logn)
 *空间复杂度:O(1)
 *稳定性:不稳定
 */
 public static void shiftDown(int[] array,int parent,int len){
        int child = 2*parent+1;
        while (child < len){
            if (child+1 < len && array[child+1] > array[child]) {
                child++;
            }
            if(array[parent]  < array[child]){
                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);

        }
    }
    public static void heapSort(int[] array){
        //大根堆
        createHeap(array);
        int end = array.length-1;
        while (end > 0){
            int tmp = array[0];
            array[0] = array[end];
            array[end] = tmp;
            shiftDown(array,0,end);
            end--;
        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值