各种排序算法,数据结构中的排序算法
- 格式:doc
- 大小:88.00 KB
- 文档页数:10
1.直接插入排序
//InsertSort.cpp
//This function is to sort SqList
# include
# include
# define MAXSIZE 20
# define MAX_LENGTH 100
typedef 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[0]=L.r[i]; for(j=i-1;L.r[0] L.r[j+1]=L.r[j]; L.r[j+1]=L.r[0]; } } void main() //main() function { int i; SqList L; cout< cout< cout< for(i=1;i<=L.length;++i) { cout<<"Please input the "< cin>>L.r[i]; } cout< for(i=1;i<=L.length;i++) cout< InsertSort(L); //call InsertSort() cout< for(i=1;i<=L.length;i++) cout< cout< getch(); } //main() end 2.希尔排序 //Shellinert.cpp //This function is Shell sort # include # include # define MAXSIZE 20 # define OK 1 # define ERROR 0 typedef 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[0]=L.r[i]; for(j=i-dk;j>0&&(L.r[0] 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< cout< cout< for(i=1;i<=L.length;i++) cout< Shellinsert(L,dk); //call Shellinsert() cout< for(i=1;i<=L.length;i++) cout< cout< getch(); } //main() end 3.冒泡排序 //BubbleSort.cpp //This function is to sort SqList # include # include # define MAXSIZE 20 # define MAX_LENGTH 100 typedef int RedType; typedef struct //define structure SqList { RedType r[MAXSIZE+1]; int length; }SqList;