快速排序
package sort;
import java.util.Arrays;
public class QuickSort2 {
public void quicksort(int arr[],int low,int high){
//迭代
if (low<high) {
int mid=getmid(arr,low,high);
quicksort(arr, 0, mid-1);
quicksort(arr, mid+1, high);
}
}
private int getmid(int[] arr, int low, int high) {
int key=arr[low];
if (arr.length>0) {
while (low<high&&key<=arr[high]) {
high--;
}
arr[low]=arr[high];
while (low<high&&key>=arr[low]) {
low++;
}
arr[high]=arr[low];
}
arr[low]=key;
return low;
}
public static void main(String[] args) {
QuickSort2 quicksort=new QuickSort2();
int arr[]={10,5,-2,25,30,45,-20};
quicksort.quicksort(arr, 0, arr.length-1);
System.out.println(Arrays.toString(arr));
}
}