2、从插入排序、交换排序、选择排序、归并排序、分配排序中任选一个,写出代码,还有画出程序的运行图
发布网友
发布时间:2022-05-03 01:59
我来回答
共1个回答
热心网友
时间:2023-10-03 08:19
#include<iostream>//归并排序
using namespace std;
#define MAXSIZE 20
struct RedType{
int key;
char info;
};
struct SqList{
RedType r[MAXSIZE+1];
int length;
};
bool LQ(int a,int b)
{
if(a<=b) return true;
else return false;
}
void Merge(RedType SR[],RedType TR[],int i,int m,int n)
{ int j,p,q,k;
for( j=m+1,k=i;i<=m&&j<=n;++k)
{
if(LQ(SR[i].key,SR[j].key)) TR[k]=SR[i++];
else TR[k]=SR[j++];
}
if(i<=m)
for(p=i;p<=m;p++,k++)
TR[k]=SR[p++];
if(j<=n)
for(q=j;q<=n;q++,k++)
TR[k]=SR[q++];
}
void Msort(RedType SR[],RedType TR1[],int s,int t)
{ int m;
RedType TR2[MAXSIZE+1];
if(s==t) TR1[s]=SR[s];
else{
m=(s+t)/2;
Msort(SR,TR2,s,m);
Msort(SR,TR2,m+1,t);
Merge(TR2,TR1,s,m,t);
}
}//Msort
void MergeSort(SqList &L)
{
Msort(L.r,L.r,1,L.length);
}
int main()
{ system("color 5F");
cout<<"Wish me succeed!"<<endl;
SqList L;
cout<<"Please enter the length:"<<endl;
cin>>L.length;
cout<<"\nPlease enter the key:"<<endl;
for(int i=1;i<=L.length;i++)
{
cin>>L.r[i].key;
}
cout<<"Please enter the information:"<<endl;
for(int k=1;k<=L.length;k++)
{
cin>>L.r[k].info;
}
MergeSort(L);
cout<<"The information after InsertSort:"<<endl;
for(int p=1;p<=L.length;p++)
{
cout<<L.r[p].info<<" ";
}
cout<<endl;
return 0;
} //程序图自己应该可以画吧?嘻嘻