快速排序 QuickSort Java代码

来源:互联网 发布:相机防抖算法 编辑:程序博客网 时间:2024/05/22 09:21
public class QuickSort{public static void main(String[] args){int[] test = {9,8,45,6,5,4,3,2,4,5,67,4,2,1,0};quickSort(test);for(int show : test){System.out.println(show);}}public static void swap(int[] nums, int i, int j){int temp = nums[i];nums[i] = nums[j];nums[j] = temp;}public static void quickSort(int[] data){doQuickSort(data, 0, data.length-1);}public static void doQuickSort(int[] data, int start, int end){if(start>=end) return;int flag = start;int low = start;int high = end + 1;while(true){while(++low < end && data[low] <= data[flag]);while(--high > start && data[high] >= data[flag]);if(low >=high) break;swap(data, low, high);}swap(data, flag, high);//关键,用highdoQuickSort(data, start, high - 1);doQuickSort(data, high + 1, end);}}

0 0
原创粉丝点击