标签:java
抽空温习一下面试实习的东西,从简单的快排开始吧。
//快排,分治策略
public static void quicksort(int[] a, int left, int right){
if(left<right){
return;
}
//分
int index = partition(a,left,right);
//治
quicksort(a,left,index-1);
quicksort(a,index+1,right);
}
/**
* 把一个数组以index为界分为左右两部分,index左边的数小于等于index,index右边的数大于index
* @param a
* @param left
* @param right
* @return index
*/
public static int partition(int[] a, int left, int right){
int povit = a[left];
int low = left;
int high = right;
while(low<high){
while(low<high && a[high]>povit){
high--;
}
if(low<high){
a[low] = a[high];
}
//<=号别只写<
while(low<high && a[low]<=povit){
low++;
}
if(low<high){
a[high] = a[low];
}
}
a[low] = povit;
return low;
}
标签:java
原文地址:http://blog.csdn.net/zhuyunhe/article/details/46414993