排序算法--快速排序

思路一:
指针交换法
45(基准值)

45 23 1 4 56 3 1

1 23 1 4 56 3 45

1 23 1 4 45 3 56

1 23 1 4 3 45 56    

1 23 1 4 3


思路二:
挖坑法(--代表新的坑)
45  (a[high]小于基准值,则将a[high] 赋值到 a[low];a[low]大于基准值 ,则将a[low]赋值到a[high],高低指针碰撞,则确定基准值的位置)

--(low) 23 1 4 56 3 1   

1(low) 23 1 4 56 3 --(high)

1 23 1 4 --(low) 3 56(high)

1 23 1 4 3(low) --(high) 56

1 23 1 4 3 45(low && high) 56

 

public class QuickSort {

    public static void main(String[] args) {
        int[] a = {45,23,1,4,56,3,1,1,44,56,77,8,34,0,90};
        quickSort(a,0,a.length-1);
        for (int i : a) {
            System.out.println(i);
        }
    }

    public static void quickSort(int[] a,int start,int end){

        if (end - start <= 1) {
            return;
        }

        int index = a[start];

        int low = start;
        int high = end;

        while (low<high){
            while(a[high] >= index && high > low){
                high--;
            }
            if (high>low){
                int temp = a[low];
                a[low] = a[high];
                a[high] = temp;
            }

            while(a[low] <= index && low < high){
                low++;
            }
            if (low < high){
                int temp = a[low];
                a[low] = a[high];
                a[high] = temp;
            }
        }
        quickSort(a,start,low-1);
        quickSort(a,low+1,end);
    }

    public static void quickSort1(int[] a,int start,int end){

        if (end - start <= 1) {
            return;
        }

        int index = a[start];

        int low = start;
        int high = end;

        while (low<high){
            while(a[high] >= index && high > low){
                high--;
            }
            if (high>low){
                a[low] = a[high];
            }

            while(a[low] <= index && low < high){
                low++;
            }
            if (low < high){
                a[high] = a[low];
            }
        }

        a[low] = index;

        quickSort(a,start,low-1);
        quickSort(a,low+1,end);

    }
}

 

输出结果:

0
1
1
1
3
4
8
23
34
44
45
56
56
77
90

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值