package quickSort;
import Utils.ArrayUtils;
public class QuickSort {
public static int getIndex(int[] srcArr,int left,int right) {
int key = srcArr[left];
while(left<right){
while(srcArr[right]>key&&left<right)
right--;
srcArr[left]=srcArr[right];
while(srcArr[left]<key&&left<right)
left++;
srcArr[right]=srcArr[left];
}
srcArr[left]=key;
return left;
}
//递归,找出口
public static void quickSort(int[] arr,int left,int right){
if(left>=right) {
return;
}
else {
int point = getIndex(arr, left, right);
quickSort(arr, left, point-1);
quickSort(arr, point+1, right);
}
}
public static void main(String[] args) {
int[] srcArr = ArrayUtils.getArray(6);
for(int n:srcArr)
System.out.print(n+" ");
System.out.println();
quickSort(srcArr, 0, srcArr.length-1);
for(int n:srcArr)
System.out.print(n+" ");
}
}
工具类:产生随机数.
package Utils;
import java.util.Random;
public class ArrayUtils {
public static int[] getArray(int len) {
Random random = new Random();
int[] a=new int[len];
for(int i=0;i<len;i++) {
a[i]=random.nextInt(100);
}
return a;
}
public static void printArray(int[] arr){
for(int a:arr)
System.out.print(a+" ");
System.out.println();
}
}