/*快速排序:*/
package sorts;
public class QuickSort{
public int[] quick(int[] data) {
quickSort(data,0,data.length-1);
return data;
}
private void quickSort(int[] data,int i,int j){
int pivotIndex=(i+j)/2;
swap(data,pivotIndex,j);
int k=partition(data,i-1,j,data[j]);
swap(data,k,j);
if((k-i)>1) quickSort(data,i,k-1);
if((j-k)>1) quickSort(data,k+1,j);
}
private int partition(int[] data, int l, int r,int pivot) {
do{
while(data[++l]<pivot);
while((r!=0)&&data[--r]>pivot);
swap(data,l,r);
}
while(l<r);
swap(data,l,r);
return l;
}
public void swap(int[] data, int i, int j) {
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
/*测试方法*/
package sorts;
public class TestSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a={1,4,3,2,6,7,5,8,9,0};
/* BubbleSort bubble=new BubbleSort();
bubble.Bubble(a);*/
/* SelectionSort select=new SelectionSort();
select.Select(a);*/
/* ShellSort shell=new ShellSort();
shell.Shell(a);*/
QuickSort quick=new QuickSort();
quick.quick(a);
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
}