插入排序

#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;

}

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