/**
* @author : xxj
* @date : 2012-7-24
* @time : 下午8:13:13
* @describe : 快速排序算法(JAVA)
*/
public class QuickSort {
public static int[] quickSort(int[] numbers){
if(numbers == null || numbers.length < 2){
return numbers;
}
int i = 0;
int j = numbers.length - 1;
int temp;
while(j != i){
for(int m = j; m <= j && m >= i ; m --){
j = m;
if(numbers[i] > numbers[m]){
temp = numbers[i];
numbers[i] = numbers[m];
numbers[m] = temp;
break;
}
}
if(i == j){
break;
}
for(int n = i; n >= i && n <= j; n ++){
i = n;
if(numbers[j] < numbers[n]){
temp = numbers[j];
numbers[j] = numbers[n];
numbers[n] = temp;
break;
}
}
}
int[] numbersLeft = new int[i];
for(int x = 0; x < i; x ++){
numbersLeft[x] = numbers[x];
}
numbersLeft = quickSort(numbersLeft);
for(int x = 0; x < i; x ++){
numbers[x] = numbersLeft[x];
}
int[] numbersRight = new int[numbers.length - i - 1];
for(int x = 0; x < numbers.length - i - 1; x ++){
numbersRight[x] = numbers[i + x + 1];
}
numbersRight = quickSort(numbersRight);
for(int x = i + 1; x < numbers.length; x ++){
numbers[x] = numbersRight[x - i - 1];
}
return numbers;
}
public static void main(String[] args) {
//printArray(quickSort(new int[]{49,38,65,97,76,13,27}));
printArray(quickSort(new int[]{429,318,625,197,176,213,327}));
}
public static void printArray(int[] numbers){
for(int i = 0; i < numbers.length; i ++){
System.out.print(numbers[i] + " ");
}
System.out.println("");
}
}
分享到:
2012-07-24 23:28
浏览 6225
评论
1 楼
zhoulujun007
2015-05-07
尼玛,算法验证不正确 176, 197, 197, 197, 327, 429, 625