码迷,mamicode.com
首页 > 编程语言 > 详细

快速排序小技巧

时间:2018-11-16 21:36:26      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:public   class   static   private   get   快速排序   quick   out   quic   

public class QuickSort {
public static void main(String[] args) {

int[] arr = {1,11,21,31,41,51,61,71,81,91};
quick(arr);
for (int i : arr) {
System.out.print(" "+i);

//运行结果是(1,11,21,31,41,51,61,71,81,91)
}
}

private static void quick(int[] arr) {
if (arr.length > 0) {
quickSort(arr,0,arr.length-1);
}
}

private static void quickSort(int[] arr, int head, int foot){
if (head < foot) {
int middle = getMiddle(arr, head, foot);
quickSort(arr,0,middle-1);
quickSort(arr,middle+1,foot);
}
}

private static int getMiddle(int[] arr, int head, int foot) {
int temp = arr[head];
while (foot > head) {
while(foot > head && arr[foot]>=temp){
foot--;
}
arr[head] = arr[foot];
while(foot > head && arr[head]<=temp){
head++;
}
arr[foot] = arr[head];
}
arr[head] = temp;
return head;
}
}

快速排序小技巧

标签:public   class   static   private   get   快速排序   quick   out   quic   

原文地址:https://www.cnblogs.com/gzsba/p/9971687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!