方法一:
public class QuickSortExp1{
public static void main(String[] args){
int[] sortArray = new int[]{5,7,4,2,9,8,3,6};
System.out.println("before sorting ,the numbers are:");
show(sortArray);
quickSort(sortArray,0,sortArray.length-1);
System.out.println("after sorting,the numbers are:");
show(sortArray);
}
public static void quickSort(int[] intArray,int left,int right){
if(left
int partValue = intArray[left];
int low = left;
int high = right;
while(low < high){
while(low partValue){
high--;
}
intArray[low] = intArray[high];
while(low
low++;
}
intArray[high] = intArray[low];
}
intArray[low] = partValue;
quickSort(intArray,left,low-1);
quickSort(intArray,low+1,right);
}
}
public static void show(int[] intArray){
for(int i=0;i
System.out.print(intArray[i]+"\t");
}
System.out.println();
}
}
方法二:
import java.util.*;
public class QuickSortExp2{
public static void main(String[] args){
List sortList = new ArrayList();
sortList.add(5);
sortList.add(7);
sortList.add(4);
sortList.add(2);
sortList.add(9);
sortList.add(8);
sortList.add(3);
sortList.add(6);
System.out.println("before sorting ,the numbers are:");
show(sortList);
quickSort(sortList);
System.out.println("after sorting,the numbers are:");
show(sortList);
}
public static void quickSort(List sortList){
if(sortList.size() <= 1){
return;
} else if(sortList.size() == 2){
if(sortList.get(0)>sortList.get(1)){
int temp = sortList.get(0);
sortList.set(0,sortList.get(1));
sortList.set(1,temp);
}
} else{
List leftList = new ArrayList();
List rightList = new ArrayList();
int partValue = sortList.get(0);
for(int i=1;i
if(sortList.get(i)< partValue){
leftList.add(sortList.get(i));
} else{
rightList.add(sortList.get(i));
}
}
quickSort(leftList);
quickSort(rightList);
sortList.clear();
sortList.addAll(leftList);
sortList.add(partValue);
sortList.addAll(rightList);
leftList.clear();
rightList.clear();
}
}
public static void show(List sortList){
for(Integer i:sortList){
System.out.print(i+"\t");
}
System.out.println();
}
}