直接插入排序

1.基本思想:
        将一个记录插入到已排序好的有序表中,从而得到一个新记录数增1的有序表。即:先将序列的第1个记录看成是一个
    有序的子序列,然后从第2个记录逐个进行插入,知道整个序列有序为止。
    要点:设立哨兵,作为临时存储和判断数组边界之用。
    
    如果碰见一个和插入元素相等的,那么插入元素把想插入的元素放在相等元素的后面,所以,相等元素的前后顺序没有改变,
    从原无序序列出去的排序就是排好序后的顺序,所以插入排序时稳定的。
2。算法实现:
#include <iostream>//修订版,订正过,12.18
using namespace std;
int main()
{
    int a[] = {98,76,109,34,67,190,80,12,14,89,1};
    int k = sizeof(a)/sizeof(a[0]);
    int j;
    for(int i=1;i<k; i++)
    {
        if(a[i]<a[i-1])
        {
            int temp = a[i];
            for(j=i-1;j>=0 && a[j]>temp; j--)
            {
                a[j+1] = a[j];
            }
            a[j+1] = temp;
        }
    }
    for(int f=0;f<k;f++)
        cout<<a[f]<<" ";
    cout<<endl;
    return 0;
}

The future's not set,there is no fate but what we make for ourselves.
原文地址:https://www.cnblogs.com/wang1994/p/9115451.html