最大的時(shí)間復(fù)雜度為On2,即數(shù)組的數(shù)降序排列時(shí),在某些數(shù)據(jù)中,該排序法有較高的效率.
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int a[12];
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 1; i < n; i++) {
int v = a[i];
int j = i - 1;
while (j >= 0 && a[j] > v) {
a[j + 1] = a[j];
j--;
a[j + 1] = v;
}
}
for (int i = 0; i < n; i++)
cout << a[i];
return 0;
}