头文件:
#include <iostream> using namespace std; #define MAX 10 typedef struct { int r[MAX+1]; }Sqlist; // 比较大小并插入 void InsertSort(Sqlist &sl, int n) { int j; for (int i = 2; i < 7; ++i) { if (sl.r[i] < sl.r[i - 1]) { sl.r[0] = sl.r[i]; for (j = i; sl.r[0] < sl.r[j-1]; --j) { sl.r[j] = sl.r[j - 1]; } sl.r[j] = sl.r[0]; } } }
主函数:
#include "InsertSort_0.h" int main() { // 0为哨兵位,起到两个作用:1、临时空间 2、保证不越界 Sqlist sq = { 0, 21, 25, 49, 25, 16, 8 }; InsertSort(sq, 7); for (int i = 1; i < 7; ++i) { cout << sq.r[i] << " "; } cout << endl; return 0; }
原文地址:http://blog.csdn.net/zhaoyaqian552/article/details/46608791