public class Quick {
public static void main(String[] args) {
int[] a = {3,2,5,1,7,8,10};
quick(a,0,a.length-1);
for(int b : a)
System.out.println(b);
}
public static int partition(int[] a, int p, int q){
int pivot = a[p];
int temp;
int i = p;
for(int j = p+1;j<a.length;j++) {
if(a[j]<=pivot){
temp=a[i+1];
a[i+1]=a[j];
a[j] = temp;
i++;
}
}
temp = a[i];
a[i] = a[p];
a[p]=temp;
return i;
}
public static void quick(int[] a, int p, int q) {
if(p<q) {
int r = partition(a,p,q);
quick(a,p,r-1);
quick(a,r+1,q);
}
}
}
No comments:
Post a Comment