DS内排—2-路归并排序
使用非递归思路
#include <iostream>
#include <vector>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<string> res(n);
for (int i = 0;i < n;i++) {
cin >> res[i];
}
int len = 1;
while (len <= n) {
vector<string> tmp = res;
for (int i = 0;i < n;i += 2*len) {
int i1 = i;
int j = i + len ;
int mid = j;
int k = i;
while (i1 < mid&&i1<n && j < mid + len && j < n) {
res[k++] = tmp[i1] > tmp[j] ? tmp[i1++]:tmp[j++];
}
while (i1 < mid&&i1<n) {
res[k++] = tmp[i1++];
}
while (j < mid + len && j < n) {
res[k++] = tmp[j++];
}
}
for (int i = 0;i < n;i++) {
if (i!=0)cout << " ";
cout << res[i] ;
}
cout << endl;
len *= 2;
}
cout << endl;
}
}