算法设计技巧与分析p113算法6.5
利用数组的第一个元素对数组进行划分,在快速排序时会用到本算法。
public class SPLIT6_5 {
public static int split(int[] a,int low,int high){
int i=low;
int x=a[low];
for(int j=low+1;j<=high;j++){
if(a[j]<=x){
i++;
if(i!=j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
int temp=a[low];
a[low]=a[i];
a[i]=temp;
return i;
}
public static void main(String args[]){
int[] a={15,8,7,9,16,1};
System.out.println(split(a,0,a.length-1));
for(int i=0;i<=a.length-1;i++){
System.out.print(a[i]+" ");
}
}
}