算法:记一次快速排序

来源:互联网 发布:mac os界面 编辑:程序博客网 时间:2024/06/01 08:49
    public static void main(String[] args) {        int[] param = new int[]{1,-1,3,7,-2,8};        int[] res = sort(param,0,param.length-1);        for (int re : res) {            System.out.print(re);        }    }    private static int[] sort(int[] nums,int left,int right) {        if(left >= right){            return nums;        }        int l = left;int r = right;        int orign = nums[l];        while(l<r){            while(l<r&&orign<nums[r]){                r--;            }            if(l<r){                nums[l++] = nums[r];            }            while(l<r&& orign>nums[l]){                l++;            }            if(l<r){                nums[r--] = nums[l];            }        }        nums[l] = orign;        sort(nums,left,l-1);        sort(nums,l+1,right);        return nums;    }
原创粉丝点击