c++实验报告答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 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 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);