【排序算法五大类 总结】

一、插入排序

1.直接插入排序


import java.util.Arrays;

/**
 * @PackageName:Sort
 * @ClassName: InsertSort
 * @Description:
 * @Author zfz
 * @date 2022/5/24 8:57
 **/
public class InsertSort {
    //不带"哨兵"
    static void InsertSort(int array[], int n) {//待排序列共n个元素
        int temp, j;
        for (int i = 1; i < n; i++) {
            if (array[i] < array[i - 1]) {  //arr[i]关键字小于前驱
                temp = array[i];
                for (j = i - 1; j >= 0 && temp < array[j]; j--) {
                    array[j + 1] = array[j]; //所有大于关键字的都向后移动
                }
                array[j + 1] = temp;
            }
        }

    }

    //带哨兵  不用每轮循环都判断j>=0
    //使用哨兵第一个数组位置其实是没有使用的 因此 要排序n个元素 应给数组n+1的长度
    static void InsertSort2(int array[], int n) {//待排序列共n个元素
        int j;
        for (int i = 2; i < n; i++) {
            if (array[i] < array[i - 1]) {
                array[0] = array[i]; //复制为哨兵
                System.out.println("哨兵:"+array[0]);

                for (j = i - 1; array[0] < array[j]; --j) {
                    array[j + 1] = array[j]; //所有大于关键字的都向后移动
                }
                array[j + 1] = array[0];  //复制到插入位置
            }
        }

    }
    public static void main(String[] args) {
        int a[] = {0, 1, 5, 3, 4, 6, 9, 2,7};
        InsertSort(a, a.length);
        /** 打印:
         [0, 1, 2, 3, 4, 5, 6, 7, 9]
        */
        InsertSort2(a, a.length);
        /** 打印:
         哨兵:3
         哨兵:4
         哨兵:2
         哨兵:7
         [7, 1, 2, 3, 4, 5, 6, 7, 9]
         */

        System.out.println(Arrays.toString(a));
    }
}

2.折半插入排序

import java.util.Arrays;
/**
 * @PackageName:Sort
 * @ClassName: Insortsort2f
 * @Description:
 * @Author zfz
 * @date 2022/5/24 10:09
 **/
public class InsertSort2 {
    static void InsertSort(int arr[], int n) {
        int j, temp, low, mid, high;
        for (int i = 1; i < n; i++) {
            temp = arr[i];
            low = 0;
            high = i - 1;
            while (low <= high) {
                mid = (low + high) / 2;
                if (arr[mid] > temp) {
                    high = mid - 1;
                } else {
                    low = mid + 1;
                }
            }
             /* for (j=i;j>=low+1;--j){
                arr[j]=arr[j-1];}
            arr[low]=temp; */
            //此处可与注释部分替换
            for (j = i - 1; j >= high + 1; --j) {   //j>=high+1 防止越界   与直接排序中的j>=0 一样的道理
                arr[j + 1] = arr[j];
            }
            arr[high + 1] = temp;
        }
    }

    public static void main(String[] args) {
        int a[] = {1, 0, 5, 3, 4, 6, 9, 2, 7};
        InsertSort(a, a.length);//[0, 1, 2, 3, 4, 5, 6, 7, 9]
        System.out.println(Arrays.toString(a));
    }
}


3.希尔排序

import java.util.Arrays;
/**
 * @PackageName:Sort
 * @ClassName: ShellSort
 * @Description:
 * @Author zfz
 * @date 2022/5/24 10:44
 **/
public class ShellSort {
    //希尔排序其实是比较距离为增量的 插入排序  本质还是插入排序 
    static void ShellSort(int arr[], int n) {
        int j, temp;
        for (int gap = n / 2; gap >0; gap /= 2) { //gap为增量
            for (int i = gap; i < n; i++) {
                if (arr[i] < arr[i - gap]) {
                    temp = arr[i];
                    for (j = i - gap; j >= 0 && temp < arr[j]; j -= gap) {
                        arr[j + gap] = arr[j];
                    }
                    arr[j + gap] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        int a[] = {1, 0, 5, 3, 4, 6, 9, 2, 7};
        ShellSort(a, a.length);//[0, 1, 2, 3, 4, 5, 6, 7, 9]
        System.out.println(Arrays.toString(a));
    }
}

二、交换排序

1.冒泡排序

import java.util.Arrays;

/**
 * @PackageName:Sort
 * @ClassName: BubbleSort
 * @Description:
 * @Author zfz
 * @date 2022/5/25 10:37
 **/
public class BubbleSort {
     static  void BubbleSort1(int []a,int n){
         for (int i=0;i<n-1;i++){
             for (int j=0;j<n-i-1;j++){
                 if (a[j]>a[j+1]){ //将最大的换到最后
                     int temp=a[j];
                     a[j]=a[j+1];
                     a[j+1]=temp;
                 }
             }
         }
     }
    public static void main(String[] args) {
        int a[] = {8, 1, 5, 3, 4, 6, 9, 2,7};
        BubbleSort1(a, a.length);
        System.out.println(Arrays.toString(a));
    }
}

2.快速排序


import java.util.Arrays;

/**
 * @PackageName:Sort
 * @ClassName: QuicklySort
 * @Description:
 * @Author zfz
 * @date 2022/5/25 10:48
 **/
public class QuicklySort {
    static int a[];
    public static void quickSort(int left, int right) {
        if (left > right) {
            return;
        }
        int temp = a[left]; // 基准
        int i = left;
        int j = right;
        while (i != j) {
            // 顺序很重要,要先从右往左找
            while (a[j] >= temp && i < j){
                j--;
            }
            // 再从左往右找
            while (a[i] <= temp && i < j){
                i++;
            }
            // 交换两个数在数组中的位置
            if (i < j) {
                int t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
        // 最终将基准归位
        a[left] = a[i];
        a[i] = temp;
        quickSort(left, i - 1);// 处理左边
        quickSort(i + 1, right);// 处理右边
        return;

    }

    public static void main(String[] args) {

        a = new int[]{6, 1, 2, 7, 9, 3, 4, 5, 10, 8};
        System.out.println("未排序前:"+Arrays.toString(a));
        //[6, 1, 2, 7, 9, 3, 4, 5, 10, 8]
        quickSort(0, a.length-1);
        System.out.println("排序后:"+Arrays.toString(a));
        //[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


    }

}

三、选择排序

1.简单选择排序


import java.util.Arrays;

/**
 * @PackageName:Sort
 * @ClassName: SelectSort
 * @Description:
 * @Author zfz
 * @date 2022/5/25 11:17
 **/
public class SelectSort {


    static void SelectSort(int a[], int n) {//待排序列共n个元素
        for (int i=0;i<n-1;i++){
            int min=i;
            for (int j=i+1;j<n;j++){
                if (a[j]<a[min]){
                    min=j;
                }
            }
            int temp =a[i];
            a[i]=a[min];
            a[min]=temp;

        }

    }
    public static void main(String[] args) {
        int a[] = {0, 1, 5, 3, 4, 6, 9, 2,7};
        SelectSort(a, a.length);
        System.out.println(Arrays.toString(a));
    }
}

2.堆排序

在这里插入代码片

四、 归并排序

五、基数排序

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱喝阔落的猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值