gehongxiang
while(i A[j]=A[i]; j--; } }while(i template void Sample int i,j,k; T temp; for(i=0;i for(j=i+1;j<=n-1;j++) if(A[j] temp=A[i]; A[i]=A[k]; A[k]=temp; } } template void Sample for(int i=0;i int sel=0; Sample cout<<\原来序列:\ s.disp(); cout<<\:插入排序 1:希尔排序 2:冒泡排序 3:快速排序\\n 4:选择排序 其它退出\ gehongxiang cout<<\选择排序方法:\ cin>>sel; switch(sel) { case 0: s.insertsort(); cout<<\插入排序结果\ break; case 1: s.Shellsort(); cout<<\希尔排序结果:\ break; case 2: s.bubblesort(); cout<<\冒泡排序结果:\ break; case 3: s.quicksort(); cout<<\快速排序结果:\ break; case 4: s.selectsort(); cout<<\选择排序结果:\ break; } s.disp(); } 程序运行结果如下: 题 7. 设计一个模板类Sample,用于对一个有序数组采用二分法查找元素下标。 解: #include T A[Max]; int n; public: Sample(){} Sample(T a[],int i); int seek(T c); void disp() { gehongxiang for(int i=0;i Sample for(int j=0;j template int Sample int low=0,high=n-1,mid; while(low<=high) { mid=(low+high)/2; if(A[mid]==c) return mid; else if(A[mid] return -1; } void main() { char a[]=\ Sample cout<<\元素序列:\ cout<<\的下标:\ } 程序运行结果如下: 元素序列:a c e g k m p w x z 元素’g’的下标: 3 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库c++考试模版(3)在线全文阅读。
相关推荐: