头文件:
#include <iostream>
using namespace std;
#define MAX 10
typedef struct
{
int r[MAX];
}Sqlist;
// 交换两个数
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
return;
}
// 比较大小
void InsertSort(Sqlist &sl, int n)
{
for (int i = 1; i<6; ++i)
{
if (sl.r[i] < sl.r[i - 1])
{
for (int j = i; j>0 && sl.r[j] < sl.r[j - 1]; --j)
{
swap(sl.r[j], sl.r[j - 1]);
}
}
}
}
主函数:
#include "InsertSort.h"
int main()
{
Sqlist sq = { 21, 25, 49, 25, 16, 8 };
InsertSort(sq,6);
for (int i = 0; i < 6; ++i)
{
cout << sq.r[i] << " ";
}
cout << endl;
return 0;
}