1、快排
public static void quickSort(int[] s, int l, int r){
if(l < r){
int i = l; int j = r; int tmp = s[l];
while(i < j){
while(i < j && s[j] >= tmp){
j--;
}
if(i < j){
s[i++] = s[j];
}
while(i < j && s[i] < tmp){
i++;
}
if(i < j){
s[j--] = s[i];
}
}
s[i] = tmp;
quickSort(s, l, i-1);
quickSort(s, i+1, r);
}
}
2、归并
public static void mergeSort(int[] arr, int low, int high, int[] tmp) {
if (low < high) {
int mid = (low + high) / 2;
mergeSort(arr, low, mid, tmp);
mergeSort(arr, mid + 1, high, tmp);
merge(arr, low, mid, high, tmp);
}
}
public static void merge(int[] arr, int low, int mid, int high, int[] tmp) {
int i = 0;
int j = low;
int k = mid + 1;
while (j <= mid && k <= high) {
if (arr[j] < arr[k]) {
tmp[i++] = arr[j++];
} else {
tmp[i++] = arr[k++];
}
}
while (j <= mid) {
tmp[i++] = arr[j++];
}
while (k <= high) {
tmp[i++] = arr[k++];
}
for (int t = 0; t < i; t++) {
arr[low + t] = tmp[t];
}
}