- #include<iostream>
- using namespace std;
- //得到最大數的位數
- int getmaxbit(int date[],int la ){
- int d=1;
- int p=10;
- for(int i=0;i<la;i++){
- while(date[i]>=p){
- p=p*10;
- ++d;
- }
- }
- return d;
- }
- //基數排序
- void radixsort(int date[],int la){
- int temp[10][10]={0};
- int order[10]={0};
- int i,j,k,n,isd,s;
- k=0;
- n=1;
- s=1;
- int max=getmaxbit(date,la);
- for(int x=1;x<max;x++){
- s=s*10;
- }
- while(n<=s){
- for(i=0;i<la;i++){
- isd=((date[i]/n)%10);
- temp[isd][order[isd]]=date[i];
- order[isd]++;
- }
- for(i=0;i<10;i++){
- if(order[i]!=0)
- for(j=0;j<order[i];j++){
- date[k]=temp[i][j];
- k++;
- }
- order[i]=0;
- }
- n=n*10;
- k=0;
- }
- }
- //交換數字
- void SWAP(int& a, int& b){
- int s=a;
- a=b;
- b=s;}
- //冒泡排序
- void bubblesort(int a[],int la){
- int i,j;
- bool flag=true;
- for(i=0;i<la && flag;i++){
- flag=false;
- for(j=(la-1);j>=i;j--){
- if(a[j]>a[j+1])
- {
- SWAP(a[j],a[j+1]);
- flag=true;
- }
- }
- }
- }
- //插入排序
- void insertsort(int a[],int la ){
- int i,temp,index;
- for (i=1;i<la;i++){
- temp=a[i];
- index=i-1;
- while (index>=0&&temp<a[index]){
- a[index+1]=a[index];
- index--;
- }
- a[index+1]=temp;
- }
- }
- int main(){
- int in[10];
- int laaa;
- int sss=0;
- int d;
- cout<<"Input"<<endl;
- cin>>in[0];
- cin>>in[1];
- cin>>in[2];
- for( d=2;d<10;d++){
- while(in[d]!=0){
- cin>>in[++d];
- }
- break;
- }
- d=d-1;
- cout<<"1-冒泡排序,2-插入排序,3-基數排序"<<endl;
- cin>>laaa;
- if(laaa==1){
- cout<<"Output"<<endl;
- cout<<"冒泡排序"<<endl;
- bubblesort(in,d);
- for(int i=0;i<=d;i++){
- if(i!=d)
- cout<<in[i]<<",";
- else
- cout<<in[i]<<endl;
- }}
- else if(laaa==2){
- cout<<"Output"<<endl;
- cout<<"插入排序"<<endl;
- d++;
- sss=d-1;
- insertsort(in,d);
- for(int i=0;i<d;i++){
- if(i!=sss)
- cout<<in[i]<<",";
- else
- cout<<in[i]<<endl;
- }
- }
- else if(laaa==3){
- cout<<"Output"<<endl;
- cout<<"基數排序"<<endl;
- d++;
- sss=d-1;
- radixsort(in,d);
- for(int i=0;i<d;i++){
- if(i!=sss)
- cout<<in[i]<<",";
- else
- cout<<in[i]<<endl;
- }
- }
- else
- cout<<"輸入有誤"<<endl;
- cout<<"End"<<endl;
- return 0;
- }
- #include<iostream>
- using namespace std;
- //得到最大數的位數
- int getmaxbit(int date[],int la ){
- int d=1;
- int p=10;
- for(int i=0;i<la;i++){
- while(date[i]>=p){
- p=p*10;
- ++d;
- }
- }
- return d;
- }
- //基數排序
- void radixsort(int date[],int la){
- int temp[10][10]={0};
- int order[10]={0};
- int i,j,k,n,isd,s;
- k=0;
- n=1;
- s=1;
- int max=getmaxbit(date,la);
- for(int x=1;x<max;x++){
- s=s*10;
- }
- while(n<=s){
- for(i=0;i<la;i++){
- isd=((date[i]/n)%10);
- temp[isd][order[isd]]=date[i];
- order[isd]++;
- }
- for(i=0;i<10;i++){
- if(order[i]!=0)
- for(j=0;j<order[i];j++){
- date[k]=temp[i][j];
- k++;
- }
- order[i]=0;
- }
- n=n*10;
- k=0;
- }
- }
- //交換數字
- void SWAP(int& a, int& b){
- int s=a;
- a=b;
- b=s;}
- //冒泡排序
- void bubblesort(int a[],int la){
- int i,j;
- bool flag=true;
- for(i=0;i<la && flag;i++){
- flag=false;
- for(j=(la-1);j>=i;j--){
- if(a[j]>a[j+1])
- {
- SWAP(a[j],a[j+1]);
- flag=true;
- }
- }
- }
- }
- //插入排序
- void insertsort(int a[],int la ){
- int i,temp,index;
- for (i=1;i<la;i++){
- temp=a[i];
- index=i-1;
- while (index>=0&&temp<a[index]){
- a[index+1]=a[index];
- index--;
- }
- a[index+1]=temp;
- }
- }
- int main(){
- int in[10];
- int laaa;
- int sss=0;
- int d;
- cout<<"Input"<<endl;
- cin>>in[0];
- cin>>in[1];
- cin>>in[2];
- for( d=2;d<10;d++){
- while(in[d]!=0){
- cin>>in[++d];
- }
- break;
- }
- d=d-1;
- cout<<"1-冒泡排序,2-插入排序,3-基數排序"<<endl;
- cin>>laaa;
- if(laaa==1){
- cout<<"Output"<<endl;
- cout<<"冒泡排序"<<endl;
- bubblesort(in,d);
- for(int i=0;i<=d;i++){
- if(i!=d)
- cout<<in[i]<<",";
- else
- cout<<in[i]<<endl;
- }}
- else if(laaa==2){
- cout<<"Output"<<endl;
- cout<<"插入排序"<<endl;
- d++;
- sss=d-1;
- insertsort(in,d);
- for(int i=0;i<d;i++){
- if(i!=sss)
- cout<<in[i]<<",";
- else
- cout<<in[i]<<endl;
- }
- }
- else if(laaa==3){
- cout<<"Output"<<endl;
- cout<<"基數排序"<<endl;
- d++;
- sss=d-1;
- radixsort(in,d);
- for(int i=0;i<d;i++){
- if(i!=sss)
- cout<<in[i]<<",";
- else
- cout<<in[i]<<endl;
- }
- }
- else
- cout<<"輸入有誤"<<endl;
- cout<<"End"<<endl;
- return 0;
- }