直接插入排序是一种稳定算法。复杂地为o(n^2).
当 记录基本有序,或代排序记录较少时,他是最佳的排序算法。
#include
using namespace std;
void s_isort(int a[],int n)
{
int temp;
for(int i=1;i
{
temp=a[i];
for(int j=i-1;j>=0&&temp
a[j+1]=a[j];
a[j+1]=temp;
}
}
int main()
{ int a[]={3,7,9,6,4,23,22,35,21,2};
s_isort(a,10);
for(int i=0;i<10;i++)
cout<<a[i]<<",";
return 0;
}
当 记录基本有序,或代排序记录较少时,他是最佳的排序算法。
#include
using namespace std;
void s_isort(int a[],int n)
{
int temp;
for(int i=1;i
{
temp=a[i];
for(int j=i-1;j>=0&&temp
a[j+1]=a[j];
a[j+1]=temp;
}
}
int main()
{ int a[]={3,7,9,6,4,23,22,35,21,2};
s_isort(a,10);
for(int i=0;i<10;i++)
cout<<a[i]<<",";
return 0;
}