【数据结构】七大排序算法

1.直接插入排序:越有序越快

时间复杂度:O(n^2),最好:O(n)
空间复习度:O(1)
稳定性:稳定

算法思想:

public static void insertSort(int[] array){

    for (int i = 1; i<array.length; i++){
        int temp = array[i];
        int j = 0;
        for(j = i-1; j>= 0; j--){
            if (temp < array[j]){
                array[j+1] = array[j];
            }else{
                break;
            }
        }
        array[j+1] = temp;
    }
}

2.希尔排序(shell):直接插入排序的优化

i++;j= i-3;
增量序列为质数。
时间复杂度:O(n^1.3~1.5)
空间复杂度:O(1)
稳定性:不稳定

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

            }else{


                break;
            }
        }
        array[j+gap] = temp;

        }

    }
public static void shellSort(int[] array){
    int[] drr = {5,3,1};
    for (int i = 0; i <drr.length ; i++) {
        shell(array,drr[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[i]>array[j]){
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }

        }

    }
}

4.冒泡排序:

时间复杂度:O(n^2)
空间复杂度:O(1)
稳定性:稳定

算法思想:
比较相邻的元素。如果第一个比第二个大,就交换它们两个;
对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素应该会是最大的数;
针对所有的元素重复以上的步骤,除了最后一个

public static void bubbleSort(int[] array){
    for (int i = 0; i <array.length-1 ; i++) {
        for (int j = 0; j <array.length-i-1 ; j++) {
            if (array[j]>array[j+1]){
                int temp = array[j];
                array[j] = array[j+1];
                array[j+1] = temp;
            }

        }

    }
}

优化:如果一开始就是有序的
时间复杂度:最好时间复杂度 O(n)

public static void bubbleSort(int[] array){
    boolean swap = false;
    for (int i = 0; i <array.length-1 ; i++) {
        swap = false;
        for (int j = 0; j <array.length-i-1 ; j++) {
            if (array[j]>array[j+1]){

                int temp = array[j];
                array[j] = array[j+1];
                array[j+1] = temp;
                swap = true;
            }else {
                break;
            }


        }
        if (!swap){
            break;
        }

    }
}

5.快速排序:

时间复杂度:O(nlogn)
空间复杂度:O(n)
递归写法:

public static int partion(int[] array, int low, int high){
    int tmp = array[low];
    while (low < high){
        while (low < high && array[high]>= tmp){
            high--;
        }
        if(low >= high){
            break;
        }else{
            array[low] = array[high];

        }
        while (low < high && array[low]<= tmp){
            low++;
        }
        if(low >= high){
            break;
        }else{
            array[high] = array[low];

        }
    }
    array[low] = tmp;
    return low;

}
public static void quick(int[] array,int start,int end){
    int par = partion(array,start,end);
    if (par > start+1){
        quick(array,start,par-1);
    }
    if (par<end-1){
        quick(array,par+1,end);
    }
}

public static void quickSort(int[] array){
    quick(array,0,array.length-1);

}

如果给的序列能不能划分为两个等长的子序列,退化成冒泡函数

非递归:

public static int partion(int[] array, int low, int high){
    int tmp = array[low];
    while (low < high){
        while (low < high && array[high]>= tmp){
            high--;
        }
        if(low >= high){
            break;
        }else{
            array[low] = array[high];

        }
        while (low < high && array[low]<= tmp){
            low++;
        }
        if(low >= high){
            break;
        }else{
            array[high] = array[low];

        }
    }
    array[low] = tmp;
    return low;

}
public static void quickSort2(int[] array){
    Stack<Integer> stack = new Stack<>();
    int low = 0;
    int high = array.length-1;
    int par = partion(array,low,high);
    if (par > low+1){
        stack.push(low);
        stack.push(par-1);
    }
    if (par<high-1){
        stack.push(par+1);
        stack.push(high);
    }
    while (!stack.empty()){
        high = stack.pop();
        low = stack.pop();
        par = partion(array,low,high);
        if (par > low+1){
            stack.push(low);
            stack.push(par-1);
        }
        if (par<high-1){
            stack.push(par+1);
            stack.push(high);
        }
    }
}

6.归并排序:

时间复杂度:O(nlogn)
空间复杂度:O(n)
稳定性:稳定

7.堆排序:

时间复杂度:O(nlogn)
空间复杂度:O(1)
稳定性:不稳定

public void AdjustDown(int root, int len) {
    int parent = root;
    int child = 2*parent+1;
    while (child<len){
        //判断是否有两个孩子
        if (child+1<len && elem[child]<elem[child+1]){
            ++child;
            }
            //child下标存的是最大值
        if(elem[child]>elem[parent]){
            int temp = elem[child];
            elem[child] = elem[parent];
            elem[parent] = temp;

            parent = child;
            child = parent*2+1;
        }else{
            break;
        }
    }


}

public void HeapSort() {
    for (int i = (array.length-1-1)/2; i >= 0 ; i--) {
    AdjustDown(i,this.usedSize);

}
    int end = this.usedSize-1;
    while(end>0) {
        int temp = this.elem[0];
        this.elem[0] = this.elem[end];
        this.elem[end] =temp;
        AdjustDown(0,end);
        end--;


    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值