直接插入排序:
#ifdef WIN32
#define _CRT_SECURE_NO_WARNINGS
#endif // WIN32
#include<iostream>
using namespace std;
int main()
{
int a[10] = { 12,88,72,45,3,15,7,66,92,31 };
for (int watch = 1; watch < 10; watch++)//watch point 右移;
{
for (int end = watch; end > 0; end--)//watch point的前段;
{
if (a[end-1]>a[end]) //前段排序。從小到大。
{
int temp = a[end-1];
a[end-1] = a[end];
a[end] =temp;
}
}
}
//已排序
for (auto const& item : a)
{
cout << item << endl;
}
return 0;
}
原理: