MOOC清华《程序设计基础》第4章:整理扑克牌(插入排序精简版)

来源:互联网 发布:单台mysql最大tps 编辑:程序博客网 时间:2024/06/05 09:04
#include <iostream>using namespace std;int main(){int cards[13] = {101, 113, 303, 206, 405, 208, 311, 304, 410, 309, 112, 207, 402};for(int i = 1; i < 13; i++)  //枚举每张待插入的牌{int target = cards[i], pos = 0;  //以第0张牌为初始待插入位置,以当前被枚举的牌为目标对象 while(target > cards[pos])pos++;  //如果目标对象比待插入位置的牌大,待插入位置后移,直到目标对象比待插入位置的牌小 for(int j = i; j > pos; j--)cards[j] = cards[j - 1];  //从待插入位置的后一张牌到目标对象之间所有的牌整体后移 cards[pos] = target;  //把已经缓存到target中的目标对象插入到待插入位置中 }for(int i = 0; i < 13; i++)cout << cards[i] << '\t';cout << endl;return 0;}

阅读全文
0 0