数据结构《实验5》实验报告
实验项目5:快速排序
回答问题完整、实验结果(运行结果界面及源程序,运行结果界面放在前面):
#include<iostream.h>
#include<cstring>
#define STUDENT EType
#define KeyType int
struct STUDENT
{
char number[10];
char name[10];
int age;
char sex[10];
char place[10];
};
struct LinearList
{
EType *r;
int length;
int maxsize;
};
void CreatLinearList(LinearList &L,int MaxListSize)
{
L.maxsize=MaxListSize;
L.r=new EType[L.maxsize];
L.length=0;
}
bool InputLinearList(LinearList &L)
{
int i,num;
cout<<"请输入要存储元素的个数:";
cin>>num;
L.length=num;
cout<<endl;
cout<<"请输入存储元素值:";
cout<<endl;
for(i=0;i<L.length;i++)
cin>>L.r[i].age;
return 1;
}
void OutputLinearList(LinearList &L)
{
for(int i=0;i<L.length;i++)
cout<<L.r[i].age<<" ";
cout<<endl;
}
int Partition(EType r[],int low,int high)
{
KeyType StandardKey;
EType temp;
temp=r[low];
StandardKey=r[low].age;
while(low<high)
{
while(low<high&&r[high].age>=StandardKey)
high--;
r[low++]=r[high];
while(low<=high&&r[low].age<=StandardKey)
low++;
r[high--]=r[low];
}
r[--low]=temp;
return low;
}
void Qsort(EType r[],int low,int high)
{
int StandardLoc;
if(low<=high-1)
{
StandardLoc=Partition(r,low,high);
Qsort(r,low,StandardLoc-1);
Qsort(r,StandardLoc+1,high);
}
}
void QuickSort(LinearList &L)
{
Qsort(L.r,0,L.length-1);
}
int main()
{
LinearList L;
int MaxListSize=50;
CreatLinearList(L,MaxListSize);
InputLinearList(L);
cout<<endl<<endl;
cout<<"快速排序前的序列是:"<<endl;
OutputLinearList(L);
cout<<endl<<endl;
QuickSort(L);
cout<<"快速排列后的序列是:"<<endl;
OutputLinearList(L);
return 0;
}。