码迷,mamicode.com
首页 > 编程语言 > 详细

插入排序

时间:2018-05-08 20:11:25      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:依次   name   一个   include   end   out   ios   pac   元素   

#include <iostream>

using namespace std;

int main(int argc, const char * argv[]) {//以打牌为例子

    int A[6]={14,41,59,26,41,58};

    for (int i=0; i<6; i++) {。       //A里的第一个元素假设为手牌

        cout << A[i] << "  " ;    

    }

    cout << endl;

    for (int j=2; j<6; j++) {

        int key=A[j];         //key为起此时起到手中的牌

        int i=j-1;        

        while (i>0&&A[i]>key) {  //让其与手中的牌依次比较,找到正确位置后插入

            A[i+1]=A[i];

            i=i-1;

        }

        A[i+1]=key;

    }

    for (int i=0; i<6; i++) {

        cout << A[i] << "  " ;

    }

    

    return 0;

}

插入排序

标签:依次   name   一个   include   end   out   ios   pac   元素   

原文地址:https://www.cnblogs.com/zhouqianwei/p/9010587.html

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