首页 > 其他 > 详细

InsertSort

时间:2019-10-23 21:51:15      阅读:80      评论:0      收藏:0      [点我收藏+]
#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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!