快速排序1

来源:互联网 发布:软件验收意见 编辑:程序博客网 时间:2024/06/15 18:41

public class Main {    public static void sort(int[] nums, int start, int end) {        if (start >= end) {            return;        }        int i = start, j = end;        int key = nums[start];        while (i < j) {            while (nums[j] >= key && i < j) {                --j;            }            nums[i] = nums[j];            while (nums[i] <= key && i < j) {                ++i;            }            nums[j] = nums[i];        }        nums[j] = key;        sort(nums, start, j);        sort(nums, j + 1, end);    }    public static void main(String[] args) {        int[] nums = new int[]{3, 2, 4, 5, 8, 2, 6, 5, 6, 2, 4, 8, 9};        sort(nums, 0, nums.length - 1);        for (int num : nums) {            System.out.print(num + " ");        }    }}


0 0
原创粉丝点击