四种常用的排序算法

1 篇文章 0 订阅

冒泡排序法:从大到小排序

依次比较相邻的两个元素,通过一次比较把未排序序列中最大(或最小)的元素放置在未排序序列的末尾。

public class Maopao {

    public static voidmain(String[] args) {

        int[] a = { 6, 9,5, 4, 7, 6, 3 };

        for (int m : a)

            System.out.print(m + "   ");

        int t = 0;

        for (int i = 0; i < a.length - 1; i++) {

            for (int j = 0; j < a.length - 1; j++) {

                if (a[j] < a[j + 1]) {

                    t = a[j];

                    a[j] = a[j + 1];

                    a[j + 1] = t;

                }

            }

        }

        System.out.println();

        for (int n : a) {

            System.out.print(n + "   ");

        }

    }

 

}

 

 

选择排序:从小到大

每一次从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。

public class 选择排序 {

 

    public static voidmain(String[] args) {

        int[] a = { 6, 9,5, 4, 7, 6, 3 };

        for (int m : a)

            System.out.print(m + "   ");

        System.out.print("\n");

        for(int i=0;i<a.length-1;i++){

            int min = i;

            for(int j=i+1;j<a.length;j++){

                if(a[min]>a[j]){

                    min=j;

                }

            }

            if(min!=i){

                int t = a[min];

                a[min]=a[i];

                a[i]=t;

            }  

        }

        for (int m : a)

            System.out.print(m + "   ");

    }

}

 

插入法:从小到大排序

将数列分为有序和无序两个部分,每次处理就是将无序数列的第一个元素与有序数列的元素从后往前逐个进行比较,找出插入位置,将该元素插入到有序数列的合适位置中。

 public class 插入排序 {

 

    public static voidmain(String[] args) {

        int[] a = { 6, 9,5, 4, 7, 6, 3 };

        int[] b = null;

        for (int m : a)

            System.out.print(m + "   ");

        System.out.print("\n");

        for (int i = 1; i < a.lengthi++) {

            for (int j = ij > 0; j--) {

                if (a[j] < a[j - 1]) {

                    int t = a[j];

                    a[j] = a[j - 1];

                    a[j - 1] = t;

                }

            }

        }

        for (int m : a)

            System.out.print(m + "   ");

    }

}

快速排序法

packageimportment;

public class 快速排序 {

    public static voidmain(String[] args) {

        int[] a = { 6, 9,5,6,7,3, 6 };

        for (int m : a)

            System.out.print(m + "   ");

        System.out.print("\n");

        sort(a,2,5);

        for (int m : a)

            System.out.print(m + "   ");

    }

    public static void sort(int data[], int startint end) {

        if (end - start <= 0){

            return;

        }

        int last = start;

        for (int i = start + 1; i <= endi++) {

            if (data[i] < data[start]) {

                int temp = data[++last];

                data[last] = data[i];

                data[i] = temp;

            }

        }

        System.out.println(last+"::"+start);

        int temp = data[last];

        data[last] = data[start];

        data[start] = temp;

        sort(datastartlast - 1);

        sort(datalast + 1, end);

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值