码迷,mamicode.com
首页 > 其他好文 > 详细

InsertSort

时间:2019-10-23 22:36:04      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:int   for   code   class   ret   排序   key   amp   oid   

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
//插入排序-升序
void InsertSort(int a[], int n)
{
    for (int j = 1; j < n; j++)
    {
        int key = a[j];
        int i = j - 1;
        while (i >= 0 && a[i] > key)
        {
            a[i + 1] = a[i];
            i--;
        }
        a[i + 1] = key;
    }
}
int main(void)
{
    int a[] = { 4,3,2,1,0 };
    InsertSort(a, 5);
    for (int i = 0; i < 5; i++)
        cout << a[i] << endl;
    return 0;
}

InsertSort

标签:int   for   code   class   ret   排序   key   amp   oid   

原文地址:https://www.cnblogs.com/vlyf/p/11729058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!