java实现快速排序,测试数据是一万个1,网上所有版本都会栈溢出,求大佬代码,蓝桥杯ALGO-59快速排序 5
1个回答
展开全部
/**
* Created by han on 2018/3/20.
*/
public class Quicksort {
private int[] numbers;
private int number;
public void sort(int[] values) {
if (values ==null || values.length==0){
return;
}
this.numbers = values;
number = values.length;
quicksort(0, number - 1);
}
private void quicksort(int low, int high) {
int i = low, j = high;
int pivot = numbers[low + (high-low)/2];
while (i <= j) {
while (numbers[i] < pivot) {
i++;
}
while (numbers[j] > pivot) {
j--;
}
if (i <= j) {
exchange(i, j);
i++;
j--;
}
}
if (low < j)
quicksort(low, j);
if (i < high)
quicksort(i, high);
}
private void exchange(int i, int j) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
public static void main(String[] args) {
Quicksort quicksort = new Quicksort();
int[] val = new int[10000];
for (int i = 0; i <10000 ; i++) {
val[i] = i+1;
}
quicksort.sort(val);
for (int i = 0; i < 10000; i++) {
System.out.println(quicksort.numbers[i]);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询