public class Example {
public static boolean less(Comparable v1,Comparable w1){
int v=Integer.parseInt((String)v1);
int w=Integer.parseInt((String)w1);
if (v<w)
return true;
else
return false;
}
protected static void each(Comparable[] a,int i,int j){
Comparable t =a[i];
a[i]=a[j];
a[j]=t;
}
protected void show(Comparable[] a){
for (int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
public static boolean isSorted(Comparable[] a){
for (int i=0;i<a.length;i++){
if (less(a[i],a[i-1])){
return false;
}
}
return true;
}
}
public class Example05 extends Example {
/**
* 快速排序
* @param a
* @param lo
* @param hi
* @return
*/
int partion(Comparable[]a,int lo,int hi) {
int i = lo, j = hi + 1;
Comparable v = a[lo];
//从左右分别找出比v小的和比v大的,然后互换位置
while (true) {
while (less(a[++i], v))
if (i == hi) break;
while (less(v, a[--j]))
if (j == lo) break;
if (i >= j) break;
each(a, i, j);
}
each(a, lo, j);
return j;
}
void sort05(Comparable[]a,int lo,int hi){
if (hi<=lo){
return;
}
int j=partion(a,lo,hi);
sort05(a,lo,j-1);
sort05(a,j+1,hi);
}
public static void main(String[] args) {
String []a={"3","5","6","4","2","1"};
System.out.println(a);
new Example05().sort05(a,0,5);
for (int i=0;i<a.length;i++){
System.out.println(a[i]);
}
}
}