public static void quickSort(int[] arr,int begin,int end){
if(begin>=end) return; //不可缺少??? 递归的结束条件
int idx=partion(arr,begin,end);
quickSort(arr,begin,idx-1);
quickSort(arr,idx+1,end);
}
private static int partion(int[] arr,int lo,int hi){
int key=arr[lo];
while(lo<hi){
while(arr[hi]>=key&&lo<hi){
hi--;
}
arr[lo]=arr[hi];
while(arr[lo]<=key&&lo<hi){
lo++;
}
arr[hi]=arr[lo];
}
arr[hi]=key;
return hi;
}
public static void InsertSort(int[] arr){
for(int j=1;j<arr.length;j++){
int i=j-1;
int key=arr[j];
while(i>0&&arr[i]>key){
arr[i+1]=arr[i];
i--;
}
arr[i+1]=key;
}
}
public static void MergeSort(int[] arr,int low,int high){
int mid=low+(high-low)/2;
MergeSort(arr,low,mid);
MergeSort(arr,mid+1,high);
Merge(arr,low,mid,high);
}
private static void Merge(int[] arr,int low,int mid,int high){
int[] temp=new int[high-low+1];
int i=low;
int j=mid+1;
int k=0;
while(i<mid&&j<high){
if(arr[i]<arr[j]){
temp[k++]=arr[i];
}else{
temp[k++]=arr[j];
}
}
while(i<mid){
temp[k++]=arr[i++];
}
while(j<high){
temp[k++]=arr[j++];
}
for(int m=0;m<temp.length;m++){
arr[m]=temp[m];
}
}
网友评论