说明
第一行 数据组数n
后面n行数据
输入示例
2
3 5 7 65 1 56 888 10 9884
6 1 4 7 66 106 2 3 700 11
输出示例
1 3 5 7 10 56 65 888 9884
1 2 3 4 7 11 66 106 700
代码
#include <bits/stdc++.h>
using namespace std;
int findSize(vector<int> &a, int value, int left, int right)
{
while (left <= right)
{
int mid = left + (right - left) / 2;
if (a[mid] == value)
return mid;
else if (a[mid] < value)
left = mid + 1;
else
right = mid - 1;
}
return left;
}
void insertionSort(vector<int> &a)
{
for (int i = 1; i < a.size(); ++i)
{
int key = a[i];
int j = i - 1;
int insertPosition = findSize(a, key, 0, i - 1);
while (j >= insertPosition)
{
a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
}
}
int main()
{
int n;
cin >> n;
while (n--)
{
getchar();
string str;
int num;
getline(cin, str);
vector<int> a;
istringstream iss(str);
while (iss >> num)
{
a.push_back(num);
}
insertionSort(a);
for (int i : a)
{
cout << i << " ";
}
cout << '\n';
}
return 0;
}