64,654
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<cstdio>
#include<windows.h>
using namespace std;
#define MAXSIZE 20
#define LT(a,b) ((a)<(b))
typedef int keyType;
typedef int InfoType;
typedef struct{
keyType key;
InfoType otherinfo;
}RedType;
typedef struct{
RedType r[MAXSIZE+1];
int length;
}SqList;
//输出
void out(SqList &s){
cout<<"\n-------------------"<<endl;
for(int i=1;i<=s.length;i++)
cout<<setw(4)<<s.r[i].key;
cout<<"\n-------------------"<<endl;
}
//插入排序
void InsertSortRef(SqList &L){
int i,j;
for(i=2;i<=L.length;++i){
if(LT(L.r[i].key,L.r[i-1].key)){
L.r[0]=L.r[i];
for(j=i-1;LT(L.r[0].key,L.r[j].key);--j)
L.r[j+i]=L.r[j];
L.r[j+1]=L.r[0];
}
}
}
void InsertSortPrt(SqList *L) {
int i,j;
for(i=2;i<=L->length;++i)
if(LT(L->r[i].key,L->r[i-1].key)){
L->r[0]=L->r[i];
for(j=i-1;LT(L->r[0].key,L->r[j].key);--j)
L->r[j+1]=L->r[j];
L->r[j+1]=L->r[0];
}
}
int main(int argc,char *argv[]){
SqList s;
cout<<"运行结果:\n";
srand(time(0));
cout<<"排序前数组a:\n";
s.length=6;
s.r[1].key=15;
s.r[2].key=65;
s.r[3].key=81;
s.r[4].key=16;
s.r[5].key=26;
s.r[6].key=85;
out(s);
//传递引用是排序错误-------------------------------为什么,,求大神解答
//InsertSortRef(s);
//传递指针式排序正确
InsertSort1(s);
cout<<"排序后数组a:\n";
out(s);
cout<<endl;
system("pause");