java用quicksort对一个储存10个数字的数组进行排序 急急急急啊
发布网友
发布时间:2022-05-26 12:51
我来回答
共1个回答
热心网友
时间:2023-10-17 22:22
class QSort {
public static void main(String[] args) {
quicksort qs = new quicksort();
int data[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };
qs.data = data;
qs.sort(0, qs.data.length - 1);
qs.display();
}
}
class quicksort {
public int data[];
private int partition(int sortArray[], int low, int hight) {
int key = sortArray[low];
while (low < hight) {
while (low < hight && sortArray[hight] >= key)
hight--;
sortArray[low] = sortArray[hight];
while (low < hight && sortArray[low] <= key)
low++;
sortArray[hight] = sortArray[low];
}
sortArray[low] = key;
return low;
}
public void sort(int low, int hight) {
if (low < hight) {
int result = partition(data, low, hight);
sort(low, result - 1);
sort(result + 1, hight);
}
}
public void display() {
for (int i = 0; i < data.length; i++) {
System.out.print(data[i]);
System.out.print(" ");
}
}
}