四种排序方式

四种排序方式

​ 1.选择排序

​ 2.插入排序

​ 3.冒泡排序

​ 4.快速排序

选择排序

分析

选择排序是最简单的排序方式,将数组从小到大排序的思路是:

先将第一(i)个元素暂时认为是该数组中的最小值,存储在temp变量中,与其余的元素以此比较。如果有更小的值,就将更小的值存储在temp中,覆盖掉之前存储的数,一轮比较完毕后,temp中存储的已经是该数组中最小的值了,退出循环,将数组的第一个元素的值赋给数组中temp值原来所在的位置。将temp值赋给数组的第一个元素。

此时数组中的第一个值已经是该数组中的最小值了,然后变量i自增,循环上述步骤,直到该数组排序完毕。

注意:因为变量的作用域的原因,设置变量flag记录原来temp值所在的位置,可以使代码更简单。

代码
public class selectSort {
    public static void selectSort(int []arr){
        for(int i=0;i<arr.length;i++){
            int temp =arr[i];
            int flag =i;
            for(int j=i+1;j<arr.length;j++){
                if(temp>arr[j]){
                    temp=arr[j];
                    flag=j;
                }
            }
            arr[flag]=arr[i];
            arr[i]=temp;
            
        }
    }
}
public class test{
    public static void main(String[] args) {
        int []arr={9,8,6,5,4,8,2,3,7,10,0};
        selectSort.selectSort(arr);
        for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
    }
}

结果:0 2 3 4 5 6 7 8 8 9 10 

插入排序

分析

插入排序的思路是:

将第一个元素认为是有序数列,将随后的元素按大小插入其中。我们可以从第二个元素开始,和前一个元素进行比较,如果该元素小于前一个元素,将前一个元素向后移,然后用该元素再和前一个元素比较,如果前面没有其他元素或者前一个比他小为止。

因为前面的元素向后移时会覆盖掉该元素的位置,所以我们设置temp来记录该元素的值,为之后赋值做准备。

随后继续循环上述过程,直到排序完毕

代码
public class insertSort {
    public static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int temp = arr[i];
            int j = i;
            while (j - 1 >= 0&& arr[j - 1] > temp) {
                arr[j] = arr[j - 1];
                j--;
            }
            arr[j] = temp;
        }
    }
}

public class test{
    public static void main(String[] args) {
        int []arr={9,8,6,5,4,8,2,3,7,10,0};
        insertSort.insertSort(arr);
        for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
    }
}

结果:0 2 3 4 5 6 7 8 8 9 10 

冒泡排序

分析

冒泡排序的思路是:

从小到大为例,从前往后,相邻两个数比较大小。前数比后数大则两者交换位置。直到最后,此时,该数组中最大的数已经在数组末尾了。

然后继续循环该过程。

代码
public class bubbleSort {
    public static void bubbleSort(int []arr){
        int temp;
        for(int i=0;i<arr.length;i++){
       for(int j=0;j<arr.length-i-1;j++){//三个数需要两次比较,四个数需要三次比较,以此类推,需要-1.
                if(arr[j]>arr[j+1]){
                    temp=arr[j];
                    arr[j]= arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}

public class test{
    public static void main(String[] args) {
        int []arr={9,8,6,5,4,8,2,3,7,10,0};
        bubbleSort.bubbleSort(arr);
        for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
    }
}

结果:0 2 3 4 5 6 7 8 8 9 10

快速排序

分析

快速排序是一种非常高效的排序方式,思路是:

先定义一个数组中的数为基准数,程序从前向后遍历一遍,将大于基准数的数放在数组左边,将小于基准数的数,放在右边。此时,基准数左边为小于基准数的元素,基准数右边为大于基准数的元素。

然后将数组分为小于基准数,和大于基准数两部分非空子集,重复上述部分,递归自身。

最后将排序好的”数组子集“们合并即可。因为此处分解出来的子集都是就地排序的,所以不需要合并。

代码
public class test {
    public static void sort(int[] arr,int low, int high ) {
        if (low >= high)
            return;
        int i = low;
        int j = high;
        int index = arr[i];
        while (i < j) {
            while (i < j && arr[j] >= index)
                j--;//满足条件从后往前遍历
            if (i < j)
                arr[i++] = arr[j];//遇到比基准数小的将该数赋给原基准值所在。
            while (i < j && arr[i] < index)
                i++;
            if (i < j)
                arr[j--] = arr[i];
        }
        arr[i] = index;
        sort(arr, low, i-1);
        sort(arr, i+1, high);
        }
    public static void quickSort(int[] arr) {
        sort(arr, 0, arr.length - 1);
    }
    public static void main(String[] args) {
        int[] arr = {9, 8, 6, 5, 4, 8, 2, 3, 7, 10, 0};
        quickSort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值