c++实验报告答案

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include

using namespace std;

struct array

{

int data;

}a[10];

int n;

//直接插入排序

void InsertSort(array a[])

{

int i,j,temp;

for(i=0;i

{

temp=a[i+1].data;

j=i;

while(j>-1&&temp<=a[j].data)

{

a[j+1].data=a[j].data;

j--;

}

a[j+1].data=temp;

}

}

//直接选择排序

void SelectSort(array a[])

{

int i,j,small;

int temp;

for(i=0;i

{

small=i;

for(j=i+1;j

if(a[j].data

small=j;

if(small!=i)

{

temp=a[i].data;

a[i].data=a[small].data ;

a[small].data =temp;

}

}

}

//冒泡排序

void BubbleSort(array a[])

{

bool change=true;

int temp;

for(int i=0;i

{

change=false;

for(int j=i+1;j

{

if(a[i].data>a[j].data)

{

temp=a[i].data;

a[i].data=a[j].data;

a[j].data=temp;

}

}

}

}

//递归的快速排序

int QKPass(array a[],int low,int high)

{

int x;

x=a[low].data;

while(low

{

while(low=x)

high--;

if(low

{

a[low].data=a[high].data;

low++;

}

while(low

low++;

if(low

{

a[high].data=a[low].data;

high--;

}

}

a[low].data=x;

return low;

}

void QKSort(array a[],int low,int high)

{

int pos;

if(low

{

pos=QKPass(a,low,high);

QKSort(a,low,pos-1);

QKSort(a,pos+1,high);

}

}

//折半查找

int BinSrch(array a[],int k)

{

int low=1;

int high=n;

int mid;

while(low<=high)

{

mid=(low+high)/2;

if(k==a[mid].data)

return(mid);

else if(k

high=mid-1;

else

low=mid+1;

}

return 0;

}

void input()

{

int i;

cout<<"输入元素个数:"<

cin>>n;

cout<<"输入"<

for(i=0;i

{

cin>>a[i].data;

}

}

void output()

{

int i;

for(i=0;i

{

cout<

}

cout<

}

int main()

{

int t;

do{

cout<<"主菜单如下:"<

cout<<" select 1: 输入待排序的元素值"<

cout<<" select 2: 直接插入排序"<

cout<<" select 3: 直接选择排序"<

cout<<" select 4: 冒泡排序"<

cout<<" select 5: 递归的快速排序"<

cout<<" select 6: 折半查找"<

cout<<" select 7: 显示已排序的元素值"<

cout<<" select 0: 退出"<

cout<<"input your select (0-6)"<

cin>>t;

if(t==1)

{

input();

}

if(t==2)

{

InsertSort(a);

}

if(t==3)

{

SelectSort(a);

}

if(t==4)

{

BubbleSort(a);

相关文档
最新文档