1.直接插入排序//InsertSort.cpp//This function is to sort SqList# include <iostream.h># include <conio.h># define MAXSIZE 20# define MAX_LENGTH 100typedef int RedType;typedef struct //define structure SqList{ RedType r[MAXSIZE+1];int length;}SqList;void InsertSort(SqList &L) //InsertSort() sub-function{ int i,j;for(i=2;i<=L.length;++i)if(L.r[i]<L.r[i-1]){ L.r[0]=L.r[i];for(j=i-1;L.r[0]<L.r[j];--j)L.r[j+1]=L.r[j];L.r[j+1]=L.r[0];}}void main() //main() function{ int i;SqList L;cout<<endl<<endl<<"InsertSort.cpp";cout<<endl<<"==============";cout<<endl<<"Please input the length of SqList (eg,5): "<<endl<<endl; cin>>L.length;for(i=1;i<=L.length;++i){ cout<<"Please input the "<<i<<"th integer (eg,58): ";cin>>L.r[i];}cout<<endl<<"The disordered : ";for(i=1;i<=L.length;i++)cout<<L.r[i]<<" ";InsertSort(L); //call InsertSort()cout<<endl<<"The ordered : ";for(i=1;i<=L.length;i++)cout<<L.r[i]<<" ";cout<<endl<<endl<<"...OK!...";getch();} //main() end2.希尔排序//Shellinert.cpp//This function is Shell sort# include <iostream.h># include <conio.h># define MAXSIZE 20# define OK 1# define ERROR 0typedef int RedType;typedef struct //structure SqList{ RedType r[MAXSIZE+1];int length;}SqList;void Shellinsert(SqList&L,int dk) //Shellinsert() sub-function { int i,j;for(i=dk+1;i<=L.length;++i)if(L.r[i]<L.r[i-dk]){ L.r[0]=L.r[i];for(j=i-dk;j>0&&(L.r[0]<L.r[j]);j-=dk)L.r[j+dk]=L.r[j];L.r[j+dk]=L.r[0];}}void main() //main() function{ int i,dk=5;SqList L={{0,49,38,65,97,76,13,27,49,55,4},10};cout<<endl<<endl<<"Shellinsert.cpp";cout<<endl<<"==============="<<endl;cout<<endl<<endl<<"The disordered : ";for(i=1;i<=L.length;i++)cout<<L.r[i]<<" ";Shellinsert(L,dk); //call Shellinsert()cout<<endl<<"The once ShellSorted sorted: ";for(i=1;i<=L.length;i++)cout<<L.r[i]<<" ";cout<<endl<<endl<<"...OK!...";getch();} //main() end3.冒泡排序//BubbleSort.cpp//This function is to sort SqList# include <iostream.h># include <conio.h># define MAXSIZE 20# define MAX_LENGTH 100typedef int RedType;typedef struct //define structure SqList{ RedType r[MAXSIZE+1];int length;}SqList;void BubbleSort(SqList &L){ int i,j,temp;for(i=0;i<=L.length;++i)for(j=L.length-2;j>i;--j)if(L.r[j+1]<L.r[j]){ temp=L.r[j+1];L.r[j+1]=L.r[j];L.r[j]=temp;}}void main() //main() function{ int i;SqList L;cout<<endl<<endl<<"BubbleSort.cpp";cout<<endl<<"=============="<<endl;cout<<endl<<"Please input the length of SqList (eg,5): ";cin>>L.length;L.length++; //the last is aided spacefor(i=1;i<L.length;++i){ cout<<"Please input the "<<i<<"th element of SqList (eg,58): "; cin>>L.r[i];}cout<<endl<<"The disordered : ";for(i=1;i<L.length;i++)cout<<L.r[i]<<" ";BubbleSort(L); //call BubbleSort()cout<<endl<<"The ordered : ";for(i=1;i<L.length;i++)cout<<L.r[i]<<" ";cout<<endl<<endl<<"...OK!...";getch();} //main() end4.快速排序//QuickSort.cpp//This function is to quick-sort SqList# include <iostream.h># include <conio.h># define MAXSIZE 20typedef int RedType;typedef struct //define SqList structure{ RedType r[MAXSIZE+1];int length;}SqList;int Partition(SqList &L,int low,int high) //Partition() sub-function { int pivotkey;L.r[0]=L.r[low];pivotkey=L.r[low];while(low<high){ while(low<high&&L.r[high]>=pivotkey)--high;L.r[low]=L.r[high];while(low<high&&L.r[low]<=pivotkey)++low;L.r[high]=L.r[low];}L.r[low]=L.r[0];return (low);} //Partition() endvoid Qsort(SqList &L,int low,int high) //Qsort() sub-function { int pivotloc;if(low<high){ pivotloc=Partition(L,low,high);Qsort(L,low,pivotloc-1);Qsort(L,pivotloc+1,high);}}void QuickSort(SqList &L) //QuickSort() sub-function { Qsort(L,1,L.length); //call Qsort()}void main() //main() function{ int i;SqList L={{0,49,38,65,97,76,13,27,49},8};cout<<endl<<endl<<"QuickSort.cpp";cout<<endl<<"============="<<endl;cout<<endl<<"The disordered : ";for(i=1;i<=L.length;i++)cout<<L.r[i]<<" ";QuickSort(L); //call QuickSort()cout<<endl<<"The sorted : ";for(i=1;i<=L.length;i++)cout<<L.r[i]<<" ";cout<<endl<<endl<<"...OK!...";getch();} //main() end5.直接选择排序//SelectSort.cpp//This function is to SelectSort SqList# include <iostream.h># include <conio.h># define MAXSIZE 20typedef int RedType;typedef struct //define structure SqList{ RedType r[MAXSIZE+1];int length;}SqList;void SelectSort(SqList &L) //SelectSort() sub-function{ int i,j,k,temp;for(i=0;i<L.length;++i){ k=i;for(j=i+1;j<L.length;++j)if(L.r[j]<L.r[k])k=j;if(i!=k){ temp=L.r[i];L.r[i]=L.r[k];L.r[k]=temp;}}}//SelectSort() endvoid main() //main() function{ int i;SqList L={{49,38,65,97,76,13,27,49,},8};cout<<endl<<endl<<"SelectSort.cpp";cout<<endl<<"=============="<<endl;cout<<endl<<"The disordered : ";for(i=0;i<L.length;i++)cout<<L.r[i]<<" ";SelectSort(L); //call SelectSort()cout<<endl<<"The sorted : ";for(i=0;i<L.length;i++)cout<<L.r[i]<<" ";cout<<endl<<endl<<"...OK!...";getch();} //main() end6.堆栈排序//HeapSort.cpp//This function is to HeapSort SqList# include <iostream.h># include <conio.h># define MAXSIZE 20typedef int RedType;typedef struct //define structure SqList{ RedType r[MAXSIZE+1];int length;}SqList;typedef SqList HeapType;void HeapAdjust(HeapType &H,int s,int m) //HeapAdjust() sub-function { int temp,j;temp=H.r[s];for(j=2*s;j<=m;j*=2){ if(j<m&&(H.r[j]<H.r[j+1]))++j;if(!(temp<H.r[j]))break;H.r[s]=H.r[j];s=j;}H.r[s]=temp;} //HeapAdjust() endvoid HeapSort(HeapType &H) //HeapSort() sub-function{ int i,temp;for(i=H.length/2;i>0;--i)HeapAdjust(H,i,H.length);for(i=H.length;i>1;--i){ temp=H.r[1];H.r[1]=H.r[i];H.r[i]=temp;HeapAdjust(H,1,i-1);}} //HeapSort() endvoid main() //main() function{ int i;HeapType H={{0,49,38,65,97,76,13,27,49},8};cout<<endl<<endl<<"HeapSort.cpp";cout<<endl<<"============"<<endl;cout<<endl<<"The disordered : ";for(i=1;i<=H.length;i++)cout<<H.r[i]<<" ";HeapSort(H); //call HeapSort()cout<<endl<<"The sorted : ";for(i=1;i<=H.length;i++)cout<<H.r[i]<<" ";cout<<endl<<endl<<"...OK!...";getch();} //main() end7.归并排序//MergeSort.cpp#include <iostream.h>#include <conio.h>#define MAXSIZE 20#define LENGTH 7typedef int RedType;typedef struct //SqList structure{ RedType r[MAXSIZE+1]; //Records Typeint length;}SqList;typedef SqList RcdType;void Merge(RcdType SR,RcdType &TR,int i,int m,int n) //Merge() function { int j,k;for(j=m+1,k=i;i<=m&&j<=n;++k){ if(SR.r[i]<=SR.r[j])TR.r[k]=SR.r[i++];elseTR.r[k]=SR.r[j++];}while(i<=m)TR.r[k++]=SR.r[i++];while(j<=n)TR.r[k++]=SR.r[j++];}//end of Merge() functionvoid MSort(RcdType SR,RcdType &TR1,int s, int t) //MSort() function { int m;RcdType TR2;//[LENGTH];if(s==t)TR1.r[s]=SR.r[t];else{ m=(s+t)/2;MSort(SR,TR2,s,m);MSort(SR,TR2,m+1,t);Merge(TR2,TR1,s,m,t);}//end of else}//end of MSort() functionvoid MergeSort(SqList &L) //MergeSort() function{MSort(L,L,1,L.length);}//end of MergeSort() functionvoid main() //main function{ int i;SqList L;//={{0,49,38,65,97,76,13,27,},LENGTH};cout<<"MergeSort.cpp"<<endl<<"============="<<endl<<endl;cout<<"Please input the length of SqList L: <eg. 7> ";cin>>L.length;cout<<"Please input the disordered array L.r: <eg.{49,38,65,97,76,13,27,...}>"<<endl;for(i=1;i<=L.length;i++)cin>>L.r[i];MergeSort(L);cout<<endl<<"The sorted array L.r: ";for(i=1;i<=L.length;i++)cout<<L.r[i]<<" ";cout<<endl;cout<<"...OK!..."<<endl;getch();}//end of main() function8.基数排序//Radixsort.cpp# include <iostream.h># include <stdio.h># include <conio.h># define MAX_NUM_OF_KEY 8# define RD 10# define MAX_SPACE 10000# define ERROR -1typedef int KeyType;typedef int InfoType;typedef int ArrType[RD];typedef struct SLCell{ KeyType keys[MAX_NUM_OF_KEY];InfoType otheritems;int next;}SLCell;typedef struct SLList{ SLCell r[MAX_SPACE];int keynum;int recnum;}SLList;int Succ(int j) //Succ() function{//To get the next functionj++;return (j);}//end of Succ() functionint Ord(int KeyBit) //Ord() function{int j;for(j=0;j<=RD-1&&j!=KeyBit;j++);if(j!=KeyBit) return(ERROR); //KeyBit OVERFLOW THEN ERROR else return(j);}//end of Ord() functionvoid OutExample(SLList L,int i) //OutExample() function{//////////////////// Output ////////////////int temp,k;printf("\nThe %d Collect result is: ",i);// temp=L.r[0].otheritems;// printf("%d -> ",temp);temp=L.r[0].next;printf("%d -> ",L.r[temp].otheritems);for(k=0;k<L.recnum-2;k++){ temp=L.r[temp].next;printf("%d -> ",L.r[temp].otheritems);}printf("%d",L.r[L.r[temp].next].otheritems);printf("\n");//////////////////////////////////////////////}//end of OutExample() functionvoid Distribute(SLList &L,int i,ArrType &f,ArrType &e) //Distribute() function { int j,p;for(j=0;j<RD;j++)f[j]=0;for(p=L.r[0].next;p;p=L.r[p].next){ j=Ord(L.r[p].keys[i]); //call Ord()if(!f[j])f[j]=p;elseL.r[e[j]].next=p;e[j]=p;}//end of for}//end of Distrubute() functionvoid Collect(SLList &L,int i,ArrType f,ArrType e) //Collect() function { int j,t;for(j=0;!f[j];j=Succ(j)); //Succ()L.r[0].next=f[j];t=e[j];while(j<RD-1){ for(j=Succ(j);j<RD-1&&!f[j];j=Succ(j));if(f[j]){ L.r[t].next=f[j];t=e[j];}//end of if}//end of whileL.r[t].next=0;OutExample(L,i); //Add Output Example function here}//end of Collect() functionvoid RadixSort(SLList &L){ int i;ArrType f,e;for(i=0;i<L.recnum;i++)L.r[i].next=i+1;L.r[L.recnum].next=0;for(i=0;i<L.keynum;i++){ Distribute(L,i,f,e);Collect(L,i,f,e);}//end of for}//end of RadixSort() functionvoid InitExample(SLList &L){//Take SLList L for exampleL.keynum=3; //total key number is 3L.recnum=7; //total current node is 10L.r[1].otheritems=278;L.r[2].otheritems=109;L.r[3].otheritems=163;L.r[4].otheritems=930;L.r[5].otheritems=580;L.r[6].otheritems=184;L.r[7].otheritems=505;//L.r[7].otheritems=0;cout<<"The InitExample SLList L is:"<<"278->109->163->930->580->184->505"<<endl;L.r[1].keys[0]=8;L.r[1].keys[1]=7;L.r[1].keys[2]=2;L.r[2].keys[0]=9;L.r[2].keys[1]=0;L.r[2].keys[2]=1;L.r[3].keys[0]=3;L.r[3].keys[1]=6;L.r[3].keys[2]=1;L.r[4].keys[0]=0;L.r[4].keys[1]=3;L.r[4].keys[2]=9;L.r[5].keys[0]=0;L.r[5].keys[1]=8;L.r[5].keys[2]=5;L.r[6].keys[0]=4;L.r[6].keys[1]=8;L.r[6].keys[2]=1;L.r[7].keys[0]=5;L.r[7].keys[1]=0;L.r[7].keys[2]=5;}void main() //main function{SLList L;cout<<"RadixSort.cpp"<<endl<<"============="<<endl<<endl;InitExample(L); //For exampleRadixSort(L); //RadixSortcout<<endl;getch();}。