Arithmetic

C语言-快速排序

PineappleCat · 3月6日 · 2020年 408次已读
```
/**
 * @ClassName: LeetCode
 * @Description:
 * @Author: liuzhi
 * @Date: 2019/12/21 10:50
 * @Version: 1.0
 **/
public class Demo1 {
    public static void main(String[] args) {
        int[] nums={10,30,5,80,20,4};
        kuaipai(nums,0,nums.length-1);
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i]+" ");
        }
    }
    /*
    快速排序-让数组内的元素递增排序
     */
    static void kuaipai(int[] a,int left,int right){
            int i,j;
            if(left < right){
                i= Division(a,left,right);
                kuaipai(a,left,i-1);
                kuaipai(a,i+1,right);
            }
    }
    static int Division(int[] a,int left,int right){
//分割
//int[] nums={10,30,5,80,20,4};
        int base = a[left];//基准元素
        while(left<right){
            while(left < right && a[right]>base)
                --right;//
            a[left]=a[right];
            while (left<right&&a[left] < base)
                ++left;
            a[right]=a[left];
        }
        a[left]=base;
        return left;
    }
}
```


Click here to view the copyright notice of this site(点击此处查看本站版权声明)


0 条回应

必须 注册 为本站用户, 登录 后才可以发表评论!