排序(持续更新)

都是自己写的,可能有不对的地方

1.冒泡排序

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

2.快速排序

public static void main(String[] args) throws Exception {
        int[] aa = new int[]{9, 4, 6, 8, 23, 5, 78, 44, 13, 34};
        kuaipai(aa, 0, 9);
    }

public static void kuaipai(int[] array, int i, int j) {
        if(i<j) {
            int a = i;
            int b = i + 1;
            int c = j;
            while (b < c) {
                while (array[c] > array[a]) {
                    c--;
                }
                if(c >= a && c >= b) {
                    int d = array[a];
                    array[a] = array[c];
                    array[c] = d;
                    a = c;
                    System.out.println(Arrays.toString(array));
                }
                while (array[b] < array[a]) {
                    b++;
                }
                if (b < a && b < c) {
                    int d = array[a];
                    array[a] = array[b];
                    array[b] = d;
                    a = b;
                    System.out.println(Arrays.toString(array));
                }
            }
            kuaipai(array, i, a-1);
            kuaipai(array, a+1, j);
        }
    }

结果:

[5, 4, 6, 8, 23, 9, 78, 44, 13, 34]
[5, 4, 6, 8, 9, 23, 78, 44, 13, 34]
[5, 4, 6, 8, 9, 23, 78, 44, 13, 34]
[4, 5, 6, 8, 9, 23, 78, 44, 13, 34]
[4, 5, 6, 8, 9, 13, 78, 44, 23, 34]
[4, 5, 6, 8, 9, 13, 23, 44, 78, 34]
[4, 5, 6, 8, 9, 13, 23, 44, 78, 34]
[4, 5, 6, 8, 9, 13, 23, 34, 78, 44]
[4, 5, 6, 8, 9, 13, 23, 34, 44, 78]
[4, 5, 6, 8, 9, 13, 23, 34, 44, 78]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值