快速排序
/**
* 快速排序
* Created by xueping.you on 15-8-5.
*/
public class QuickSort {
private final static Logger logger = LoggerFactory.getLogger(QuickSort.class);
public static void quickSort(int []unSortArray , int left , int right){
int low = left;
int high = right;
if(low>=high)
return;
int temp = unSortArray[left];
while(left<right){
while (left<right && unSortArray[right]>=temp)right--;
unSortArray[left]=unSortArray[right];
while (left<right && unSortArray[left]<=temp)left++;
unSortArray[right]=unSortArray[left];
}
unSortArray[left]=temp;
quickSort(unSortArray, low, left);
quickSort(unSortArray, left+1, high);
}
public static void main(String []args){
int [] array = new int[]{12,10,2,45,31,56,1,9};
logger.info("before:{}", array);
quickSort(array,0 , array.length-1);
logger.info("after:{}",array);
}
}
result:
19:30:11.835 [main] INFO com.qyou.data.arithmetic.QuickSort - before:[12, 10, 2, 45, 31, 56, 1, 9]
19:30:11.845 [main] INFO com.qyou.data.arithmetic.QuickSort - after:[1, 2, 9, 10, 12, 31, 45, 56]