以下三个验证性实验都做。
(1)直接插入排序算法验证。
(2)快速排序算法验证。
(3)直接选择排序算法验证。
#include<iostream> #include<cstdlib> using namespace std; class dishizhang { public: int a[10]; int b[10]; dishizhang() { int tem; for(int i = 0 ; i < 10 ; i ++) { while(1) { int flag = 0; tem = rand() % 10; for(int j = 0 ; j < i ; j ++) { if(tem == a[j]) { flag = 1; break; } } if(flag == 0) { a[i] = tem; break; } } } cout<<"原始数列为:"; for(int i = 0 ; i < 10 ; i++) { cout<<a[i]<<" "; } cout<<endl; for(int j = 0 ; j < 10 ; j++) { b[j] = a[j]; } } void zhijiecharu() { int tem[10]; for(int i = 0 ; i < 10 ; i ++) { tem[i] = a[i]; } for(int i = 1 ; i < 10 ; i++) { int j = i-1; int temnum = tem[i]; while(j >= 0 && temnum < tem[j]) { tem[j+1] = tem[j]; j--; } tem[j+1] = temnum; } for(int i = 0 ; i < 10 ; i++) { cout<< tem[i] <<" "; } } void kuaipai(int low , int high) { if( low < high) { int tem = fuzhukuaipai( low , high); kuaipai(low , tem -1); kuaipai(tem+1,high); } } int fuzhukuaipai(int low , int high) { int tem = b[low]; while(low < high) { while(low < high && b[high] >= tem )--high; b[low] = b[high]; while(low < high && b[low] <= tem) ++low; b[high] = b[low]; } b[low] = tem; return low; } void zhijiexuanze() { int tem[10]; for(int i = 0 ; i < 10 ; i++) { tem[i] = a[i]; } int i,temnum; i = 0; int temi; while( i < 10) { int flag = 0; temnum = tem[i]; for(int j = i ; j < 10 ; j++) { if(tem[j] < temnum) { temnum = tem[j]; temi = j; flag = 1; } } int num; if(flag!=0) { num = tem[temi]; tem[temi] = tem[i]; tem[i] = num; } i++; } cout<<"直接选择后的序列是:"; for(int i = 0 ; i < 10 ; i++) { cout<<tem[i]<<" "; } } void print() { for(int i = 0 ; i < 10 ; i++) { cout<<a[i]<<" "; } } }; int main() { dishizhang duskcl; duskcl.zhijiexuanze(); }
直接插入排序 快速排序算法 直接选择排序,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/Duskcl/p/3825087.html