/**
*
*/
package com.cgs.composite;
/**
* @author 58
*
*/
public class FastSort {
public static void fastSort(int a[],int low,int high){
if(low < high){
int middle = getMiddle(a,low,high);
fastSort(a,low,middle-1);
fastSort(a,middle+1,high);
}
}
public static int getMiddle(int array[],int low ,int high){
int middle = low;
while(low < high){
for(int i=high; i >=low;i--){
if(array[i] < array[middle]){
int tmp = array[i];
array[i] = array[middle];
array[middle] = tmp;
middle = i;
high = i;
break;
}else{
high --;
}
}
for(int i=low; i <=high;i++){
if(array[i] > array[middle]){
int tmp = array[i];
array[i] = array[middle];
array[middle] = tmp;
middle = i;
low = i;
break;
}else{
low ++;
}
}
}
return middle; // 返回中轴的位置
}
public static void main(String[] args) {
int[] arr = {22,34,4,17,34,6,7,43,2,8,1};
FastSort.fastSort(arr,0,arr.length -1);
for(int i=0; i<arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}
快速排序代码
最新推荐文章于 2024-01-02 14:03:12 发布