题目描述
输入一组数据,用快速排序进行升序排序。
输入
测试次数t
每组测试数据为:
数据个数n,n个数字
输出
对每组测试数据,输出快速排序每趟排好的数字及位置(位置从1开始)。不同组测试数据的输出以空行分隔。
样例输入
2
6 111 22 6 444 333 55
8 77 555 33 1 444 77 10 200
样例输出
111 4
55 3
6 1
22 2
333 5
444 6
77 4
10 2
1 1
33 3
444 7
200 6
77 5
555 8
代码
#include <iostream>
using namespace std;
int n;
int Partition(int *a, int low, int high) {
int pivot = a[low];
while (low < high) {
while (low < high && a[high] >= pivot)
high--;
a[low] = a[high];
while (low < high && a[low] < pivot)
low++;
a[high] = a[low];
}
a[high] = pivot;
cout << pivot << ' ' << high << endl;
return high;
}
void QuickSort(int *a, int low, int high) {
if (low <= high) {
int pivotpos = Partition(a, low, high);
QuickSort(a, low, pivotpos - 1);
QuickSort(a, pivotpos + 1, high);
}
}
int main() {
int t;
cin >> t;
while (t--) {
cin >> n;
int *a = new int[n + 1];
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
QuickSort(a, 1, n);
cout << endl;
delete[]a;
}
cout << endl;
return 0;
}