package test;
public class fastSort {
/**
* 取数组中的一个数作为中轴 按比此数大或者小分为左右两个数组 分别将左右两个数组再次排序 最后合并成一个数组
* @param arr
* @param arrsize
* @return
*/
public static int[] sort(int arr[], int arrsize) {
ArrCreater.printArr(arr);
if (arrsize <= 1) {
} else if (arrsize == 2) {
if (arr[1] < arr[0]) {
int temp = arr[1];
arr[1] = arr[0];
arr[0] = temp;
}
} else {
int nmb = arr[0], leftindex = 0, rightindex = 0;
int[] left = new int[arrsize - 1], right = new int[arrsize - 1];
for (int i = 1; i < arrsize; i++) {
if (arr[i] < nmb) {
left[leftindex] = arr[i];
leftindex++;
} else {
right[rightindex] = arr[i];
rightindex++;
}
}
left = sort(left, leftindex);
right = sort(right, rightindex);
for (int i = 0; i < leftindex; i++) {
arr[i] = left[i];
}
for (int i = 0; i < rightindex; i++) {
arr[leftindex + 1 + i] = right[i];
}
arr[leftindex] = nmb;
}
return arr;
}
public static void main(String args[]) {
int[] arr = ArrCreater.create(10);
ArrCreater.printArr(arr);
arr = sort(arr, arr.length);
ArrCreater.printArr(arr);
}
}