mirror of
https://github.com/This-weiyuqing/DSAndOS.git
synced 2026-10-19 03:33:54 +08:00
update
This commit is contained in:
1 parent
16d694ffe3
commit
35e5484d7c
183 files changed
+362
-7145
No files matched your search
@@ -0,0 +1,65 @@
|
||||
//算法8.1 直接插入排序
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#define MAXSIZE 20 //顺序表的最大长度
|
||||
typedef struct
|
||||
{
|
||||
int key;
|
||||
char *otherinfo;
|
||||
}ElemType;
|
||||
//顺序表的存储结构
|
||||
typedef struct
|
||||
{
|
||||
ElemType *r; //存储空间的基地址
|
||||
int length; //顺序表长度
|
||||
}SqList; //顺序表类型
|
||||
|
||||
void InsertSort(SqList &L)
|
||||
{
|
||||
//对顺序表L做直接插入排序
|
||||
int i,j;
|
||||
for(i=2;i<=L.length;++i)
|
||||
if(L.r[i].key<L.r[i-1].key)
|
||||
{ //"<",需将r[i]插入有序子表
|
||||
L.r[0]=L.r[i]; //将待插入的记录暂存到监视哨中
|
||||
L.r[i]=L.r[i-1]; //r[i-1]后移
|
||||
for(j=i-2; L.r[0].key<L.r[j].key;--j) //从后向前寻找插入位置
|
||||
L.r[j+1]=L.r[j]; //记录逐个后移,直到找到插入位置
|
||||
L.r[j+1]=L.r[0]; //将r[0]即原r[i],插入到正确位置
|
||||
} //if
|
||||
} //InsertSort
|
||||
|
||||
void Create_Sq(SqList &L)
|
||||
{
|
||||
int i,n;
|
||||
cout<<"请输入数据个数,不超过"<<MAXSIZE<<"个。"<<endl;
|
||||
cin>>n; //输入个数
|
||||
cout<<"请输入待排序的数据:\n";
|
||||
while(n>MAXSIZE)
|
||||
{
|
||||
cout<<"个数超过上限,不能超过"<<MAXSIZE<<",请重新输入"<<endl;
|
||||
cin>>n;
|
||||
}
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
cin>>L.r[i].key;
|
||||
L.length++;
|
||||
}
|
||||
}
|
||||
void show(SqList L)
|
||||
{
|
||||
int i;
|
||||
for(i=1;i<=L.length;i++)
|
||||
cout<<L.r[i].key<<endl;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
SqList L;
|
||||
L.r=new ElemType[MAXSIZE+1];
|
||||
L.length=0;
|
||||
Create_Sq(L);
|
||||
InsertSort(L);
|
||||
cout<<"排序后的结果为:"<<endl;
|
||||
show(L);
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
//算法8.10 相邻两个有序子序列的归并
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#define MAXSIZE 20 //顺序表的最大长度
|
||||
typedef struct
|
||||
{
|
||||
int key;
|
||||
char *otherinfo;
|
||||
}RedType;
|
||||
|
||||
void Create_Sq(RedType *R)
|
||||
{
|
||||
int i,n;
|
||||
cout<<"请输入数据个数,不超过"<<MAXSIZE<<"个。"<<endl;
|
||||
cin>>n; //输入个数
|
||||
cout<<"请输入待合并的数据:\n";
|
||||
while(n>MAXSIZE)
|
||||
{
|
||||
cout<<"个数超过上限,不能超过"<<MAXSIZE<<",请重新输入"<<endl;
|
||||
cin>>n;
|
||||
}
|
||||
for(i=0;i<n;i++)
|
||||
cin>>R[i].key;
|
||||
}
|
||||
|
||||
void Merge(RedType R[],RedType T[],int low,int mid,int high)
|
||||
{
|
||||
//将有序表R[low..mid]和R[mid+1..high]归并为有序表T[low..high]
|
||||
int i,j,k;
|
||||
i=low; j=mid+1;k=low;
|
||||
while(i<=mid&&j<=high)
|
||||
{
|
||||
//将R中记录由小到大地并入T中
|
||||
if(R[i].key<=R[j].key) T[k++]=R[i++];
|
||||
else T[k++]=R[j++];
|
||||
}
|
||||
while(i<=mid) //将剩余的R[low..mid]复制到T中
|
||||
T[k++]=R[i++];
|
||||
while(j<=high) //将剩余的R[j.high]复制到T中
|
||||
T[k++]=R[j++];
|
||||
}//Merge
|
||||
void show(RedType *T,int low,int high)
|
||||
{
|
||||
int i;
|
||||
for(i=low;i<=high;i++)
|
||||
cout<<T[i].key<<endl;
|
||||
}
|
||||
void main()
|
||||
{
|
||||
RedType *R=new RedType[MAXSIZE];
|
||||
RedType *T=new RedType[MAXSIZE];
|
||||
Create_Sq(R);
|
||||
int low,mid,high;
|
||||
cout<<"请输入第一个有序子序列的起点、终点下标和第二个有序子序列的终点下标:\n";
|
||||
cin>>low>>mid>>high;
|
||||
Merge(R,T,low,mid,high);
|
||||
cout<<"合并后的结果为:"<<endl;
|
||||
show(T,low,high);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
//算法8.11 归并排序
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#define MAXSIZE 20 //顺序表的最大长度
|
||||
typedef struct
|
||||
{
|
||||
int key;
|
||||
char *otherinfo;
|
||||
}RedType;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
RedType *r;
|
||||
int length;
|
||||
}SqList;
|
||||
|
||||
void Create_Sq(SqList &L)
|
||||
{
|
||||
int i,n;
|
||||
cout<<"请输入数据个数,不超过"<<MAXSIZE<<"个。"<<endl;
|
||||
cin>>n; //输入个数
|
||||
cout<<"请输入待排序的数据:\n";
|
||||
while(n>MAXSIZE)
|
||||
{
|
||||
cout<<"个数超过上限,不能超过"<<MAXSIZE<<",请重新输入"<<endl;
|
||||
cin>>n;
|
||||
}
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
cin>>L.r[i].key;
|
||||
L.length++;
|
||||
}
|
||||
}
|
||||
|
||||
//用算法8.10 相邻两个有序子序列的归并
|
||||
void Merge(RedType R[],RedType T[],int low,int mid,int high)
|
||||
{
|
||||
//将有序表R[low..mid]和R[mid+1..high]归并为有序表T[low..high]
|
||||
int i,j,k;
|
||||
i=low; j=mid+1;k=low;
|
||||
while(i<=mid&&j<=high)
|
||||
{
|
||||
//将R中记录由小到大地并入T中
|
||||
if(R[i].key<=R[j].key) T[k++]=R[i++];
|
||||
else T[k++]=R[j++];
|
||||
}
|
||||
while(i<=mid) //将剩余的R[low..mid]复制到T中
|
||||
T[k++]=R[i++];
|
||||
while(j<=high) //将剩余的R[j.high]复制到T中
|
||||
T[k++]=R[j++];
|
||||
}//Merge
|
||||
|
||||
void MSort(RedType R[],RedType T[],int low,int high)
|
||||
{
|
||||
//R[low..high]归并排序后放入T[low..high]中
|
||||
int mid;
|
||||
RedType *S=new RedType[MAXSIZE];
|
||||
if(low==high) T[low]=R[low];
|
||||
else
|
||||
{
|
||||
mid=(low+high)/2; //将当前序列一分为二,求出分裂点mid
|
||||
MSort(R,S,low,mid); //对子序列R[low..mid] 递归归并排序,结果放入S[low..mid]
|
||||
MSort(R,S,mid+1,high); //对子序列R[mid+1..high] 递归归并排序,结果放入S[mid+1..high]
|
||||
Merge(S,T,low,mid,high); //将S[low..mid]和S [mid+1..high]归并到T[low..high]
|
||||
}//else
|
||||
}// MSort
|
||||
|
||||
void MergeSort(SqList &L)
|
||||
{
|
||||
//对顺序表L做归并排序
|
||||
MSort(L.r,L.r,1,L.length);
|
||||
}//MergeSort
|
||||
void show(SqList L)
|
||||
{
|
||||
int i;
|
||||
for(i=1;i<=L.length;i++)
|
||||
cout<<L.r[i].key<<endl;
|
||||
}
|
||||
void main()
|
||||
{
|
||||
SqList R;
|
||||
R.r=new RedType[MAXSIZE+1];
|
||||
R.length=0;
|
||||
Create_Sq(R);
|
||||
MergeSort(R);
|
||||
cout<<"排序后的结果为:"<<endl;
|
||||
show(R);
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
//算法8.12 基数排序
|
||||
#include <iostream.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#define MAXNUM_KEY 8 //关键字项数的最大值
|
||||
#define RADIX 10 //关键字基数,此时是十进制整数的基数
|
||||
#define MAX_SPACE 10000
|
||||
typedef char KeysType; //定义关键字类型为字符型
|
||||
typedef int InfoType; //定义其它数据项的类型
|
||||
typedef struct
|
||||
{
|
||||
KeysType keys[MAXNUM_KEY]; //关键字
|
||||
InfoType otheritems; //其他数据项
|
||||
int next;
|
||||
}SLCell; //静态链表的结点类型
|
||||
typedef struct
|
||||
{
|
||||
SLCell r[MAX_SPACE]; //静态链表的可利用空间,r[0]为头结点
|
||||
int keynum; //记录的当前关键字个数
|
||||
int recnum; //静态链表的当前长度
|
||||
}SLList; //静态链表类型
|
||||
typedef int ArrType[RADIX]; //指针数组类型
|
||||
|
||||
void InitList(SLList *L)
|
||||
{
|
||||
//初始化静态链表L(把数组D中的数据存于L中)
|
||||
char c[MAXNUM_KEY],c1[MAXNUM_KEY];
|
||||
int i,j,n,max; //max为关键字的最大值
|
||||
max=-10000;
|
||||
cout<<"请输入数据个数,不超过"<<MAX_SPACE<<"个。\n";
|
||||
cin>>n;
|
||||
while(n>MAX_SPACE)
|
||||
{
|
||||
cout<<"您输入的个数超过上限,请重新输入,不超过"<<MAX_SPACE<<"个。\n";
|
||||
cin>>n;
|
||||
}
|
||||
int *D=new int[n];
|
||||
cout<<"请输入"<<n<<"个排排序的数据:\n";
|
||||
for(i=0;i<n;i++)
|
||||
{
|
||||
cin>>D[i];
|
||||
if(max<D[i])
|
||||
max=D[i];
|
||||
}
|
||||
(*L).keynum=(int)(ceil(log10(max)));
|
||||
(*L).recnum=n;
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
itoa(D[i-1],c,10); //将10进制整型转化为字符型,存入c
|
||||
for(j=strlen(c);j<(*L).keynum;j++) //若c的长度<max的位数,在c前补'0'
|
||||
{
|
||||
strcpy(c1,"0");
|
||||
strcat(c1,c);
|
||||
strcpy(c,c1);
|
||||
}
|
||||
for(j=0;j<(*L).keynum;j++)
|
||||
(*L).r[i].keys[j]=c[(*L).keynum-1-j];
|
||||
}
|
||||
}
|
||||
|
||||
int ord(char c)
|
||||
{
|
||||
//返回k的映射(个位整数)
|
||||
return c-'0';
|
||||
}
|
||||
void Distribute(SLCell *r,int i,ArrType &f,ArrType &e)
|
||||
{
|
||||
//静态链表L的r域中记录已按(keys[0], …, keys[i-1])有序
|
||||
//本算法按第i个关键字keys[i]建立RADIX个子表,使同一子表中记录的keys[i]相同。
|
||||
//f[0..RADIX-1]和e[0..RADIX-1]分别指向各子表中第一个和最后一个记录
|
||||
int j,p;
|
||||
for(j=0;j<RADIX;++j) f[j]=0; //各子表初始化为空表
|
||||
for(p=r[0].next;p;p=r[p].next)
|
||||
{
|
||||
j=ord(r[p].keys[i]); //ord将记录中第i个关键字映射到[0..RADIX-1]
|
||||
if(!f[j]) f[j]=p;
|
||||
else r[e[j]].next=p;
|
||||
e[j]=p; //将p所指的结点插入第j个子表中
|
||||
}//for
|
||||
}//Distribute
|
||||
|
||||
int succ(int i)
|
||||
{
|
||||
//求后继函数
|
||||
return ++i;
|
||||
}
|
||||
void Collect (SLCell *r,int i,ArrType &f,ArrType &e)
|
||||
{
|
||||
//本算法按keys[i]自小至大地将f[0..RADIX-1]所指各子表依次链接成一个链表
|
||||
//e[0..RADIX-1]为各子表的尾指针
|
||||
int j,t;
|
||||
for(j=0;!f[j];j=succ(j)); //找第一个非空子表,succ为求后继函数
|
||||
r[0].next=f[j];t=e[j]; //r[0].next指向第一个非空子表中第一个结点
|
||||
while(j<RADIX-1)
|
||||
{
|
||||
for(j=succ(j);j<RADIX-1&&!f[j];j=succ(j)) ; //找下一个非空子表
|
||||
if(f[j]) {r[t].next=f[j];t=e[j];} //链接两个非空子表
|
||||
}//while
|
||||
r[t].next=0; //t指向最后一个非空子表中的最后一个结点
|
||||
}//Collect
|
||||
|
||||
void RadixSort(SLList &L)
|
||||
{
|
||||
//L是采用静态链表表示的顺序表
|
||||
//对L做基数排序,使得L成为按关键字自小到大的有序静态链表,L.r[0]为头结点
|
||||
int i;
|
||||
ArrType f,e;
|
||||
for(i=0;i<L.recnum;++i) L.r[i].next=i+1;
|
||||
L.r[L.recnum].next = 0; //将L改造为静态链表
|
||||
for(i=0;i<L.keynum;++i)
|
||||
{
|
||||
//按最低位优先依次对各关键字进行分配和收集
|
||||
Distribute(L.r,i,f,e); //第i趟分配
|
||||
Collect(L.r,i,f,e); //第i趟收集
|
||||
}//for
|
||||
} // RadixSort
|
||||
|
||||
void print(SLList L)
|
||||
{
|
||||
//按数组序号输出静态链表
|
||||
int i,j;
|
||||
for(i=1;i<=L.recnum;i++)
|
||||
{
|
||||
for(j=L.keynum-1;j>=0;j--)
|
||||
cout<<L.r[i].keys[j];
|
||||
cout<<endl;
|
||||
}
|
||||
}
|
||||
void Sort(SLList L,int adr[])
|
||||
{
|
||||
//求得adr[1..L.length],adr[i]为静态链表L的第i个最小记录的序号
|
||||
int i=1,p=L.r[0].next;
|
||||
while(p)
|
||||
{
|
||||
adr[i++]=p;
|
||||
p=L.r[p].next;
|
||||
}
|
||||
}
|
||||
void Rearrange(SLList *L,int adr[])
|
||||
{
|
||||
//adr给出静态链表L的有序次序,即L.r[adr[i]]是第i小的记录。
|
||||
//本算法按adr重排L.r,使其有序。算法10.18(L的类型有变)
|
||||
int i,j,k;
|
||||
if(adr[i]!=i)
|
||||
{
|
||||
j=i;
|
||||
(*L).r[0]=(*L).r[i]; //暂存记录(*L).r[i]
|
||||
while(adr[j]!=i)
|
||||
{
|
||||
//调整(*L).r[adr[j]]的记录到位直到adr[j]=i为止
|
||||
k=adr[j];
|
||||
(*L).r[j]=(*L).r[k];
|
||||
adr[j]=j;
|
||||
j=k; //记录按序到位
|
||||
}
|
||||
(*L).r[j]=(*L).r[0];
|
||||
adr[j]=j;
|
||||
}
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
SLList l;
|
||||
int *adr;
|
||||
InitList(&l);
|
||||
RadixSort(l);
|
||||
adr=new int[l.recnum];
|
||||
Sort(l,adr);
|
||||
Rearrange(&l,adr);
|
||||
cout<<"排序后(重排记录):\n";
|
||||
print(l);
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
//算法8.2 折半插入排序
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#define MAXSIZE 20 //顺序表的最大长度
|
||||
typedef struct
|
||||
{
|
||||
int key;
|
||||
char *otherinfo;
|
||||
}ElemType;
|
||||
//顺序表的存储结构
|
||||
typedef struct
|
||||
{
|
||||
ElemType *r; //存储空间的基地址
|
||||
int length; //顺序表长度
|
||||
}SqList; //顺序表
|
||||
|
||||
void BInsertSort(SqList &L){
|
||||
//对顺序表L做折半插入排序
|
||||
int i,j,low,high,m;
|
||||
for(i=2;i<=L.length;++i)
|
||||
{
|
||||
L.r[0]=L.r[i]; //将待插入的记录暂存到监视哨中
|
||||
low=1; high=i-1; //置查找区间初值
|
||||
while(low<=high)
|
||||
{ //在r[low..high]中折半查找插入的位置
|
||||
m=(low+high)/2; //折半
|
||||
if(L.r[0].key<L.r[m].key) high=m-1; //插入点在前一子表
|
||||
else low=m+1; //插入点在后一子表
|
||||
}//while
|
||||
for(j=i-1;j>=high+1;--j) L.r[j+1]=L.r[j]; //记录后移
|
||||
L.r[high+1]=L.r[0]; //将r[0]即原r[i],插入到正确位置
|
||||
} //for
|
||||
} //BInsertSort
|
||||
|
||||
void Create_Sq(SqList &L)
|
||||
{
|
||||
int i,n;
|
||||
cout<<"请输入数据个数,不超过"<<MAXSIZE<<"个。"<<endl;
|
||||
cin>>n; //输入个数
|
||||
cout<<"请输入待排序的数据:\n";
|
||||
while(n>MAXSIZE)
|
||||
{
|
||||
cout<<"个数超过上限,不能超过"<<MAXSIZE<<",请重新输入"<<endl;
|
||||
cin>>n;
|
||||
}
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
cin>>L.r[i].key;
|
||||
L.length++;
|
||||
}
|
||||
}
|
||||
|
||||
void show(SqList L)
|
||||
{
|
||||
int i;
|
||||
for(i=1;i<=L.length;i++)
|
||||
cout<<L.r[i].key<<endl;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
SqList L;
|
||||
L.r=new ElemType[MAXSIZE+1];
|
||||
L.length=0;
|
||||
Create_Sq(L);
|
||||
BInsertSort(L);
|
||||
cout<<"排序后的结果为:"<<endl;
|
||||
show(L);
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
//算法8.3 希尔排序
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#define MAXSIZE 20 //顺序表的最大长度
|
||||
typedef struct
|
||||
{
|
||||
int key;
|
||||
char *otherinfo;
|
||||
}ElemType;
|
||||
//顺序表的存储结构
|
||||
typedef struct
|
||||
{
|
||||
ElemType *r; //存储空间的基地址
|
||||
int length; //顺序表长度
|
||||
}SqList; //顺序表类型
|
||||
|
||||
void ShellInsert(SqList &L,int dk)
|
||||
{
|
||||
//对顺序表L做一趟增量是dk的希尔插入排序
|
||||
int i,j;
|
||||
for(i=dk+1;i<=L.length;++i)
|
||||
if(L.r[i].key<L.r[i-dk].key)
|
||||
{ //需将L.r[i]插入有序增量子表
|
||||
L.r[0]=L.r[i]; //暂存在L.r[0]
|
||||
for(j=i-dk;j>0&& L.r[0].key<L.r[j].key;j-=dk)
|
||||
L.r[j+dk]=L.r[j]; //记录后移,直到找到插入位置
|
||||
L.r[j+dk]=L.r[0]; //将r[0]即原r[i],插入到正确位置
|
||||
} //for
|
||||
}
|
||||
//ShellInsert
|
||||
void ShellSort(SqList &L,int dt[ ],int t){
|
||||
//按增量序列dt[0..t-1]对顺序表L作t趟希尔排序
|
||||
int k;
|
||||
for(k=0;k<t;++k)
|
||||
ShellInsert(L,dt[k]); //一趟增量为dt[t]的希尔插入排序
|
||||
} //ShellSort
|
||||
|
||||
void Create_Sq(SqList &L)
|
||||
{
|
||||
int i,n;
|
||||
cout<<"请输入数据个数,不超过"<<MAXSIZE<<"个。"<<endl;
|
||||
cin>>n; //输入个数
|
||||
cout<<"请输入待排序的数据:\n";
|
||||
while(n>MAXSIZE)
|
||||
{
|
||||
cout<<"个数超过上限,不能超过"<<MAXSIZE<<",请重新输入"<<endl;
|
||||
cin>>n;
|
||||
}
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
cin>>L.r[i].key;
|
||||
L.length++;
|
||||
}
|
||||
}
|
||||
|
||||
void show(SqList L)
|
||||
{
|
||||
int i;
|
||||
for(i=1;i<=L.length;i++)
|
||||
cout<<L.r[i].key<<endl;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
SqList L;
|
||||
L.r=new ElemType[MAXSIZE+1];
|
||||
L.length=0;
|
||||
Create_Sq(L);
|
||||
|
||||
int i,t;//增量数组的长度
|
||||
int *dt=new int[MAXSIZE];//增量数组
|
||||
cout<<"请输入增量个数:\n";
|
||||
cin>>t;
|
||||
for(i=0;i<t;i++)
|
||||
{
|
||||
cout<<"第"<<i+1<<"个增量:\n";
|
||||
cin>>dt[i];
|
||||
}
|
||||
ShellSort(L,dt,t);
|
||||
cout<<"排序后的结果为:"<<endl;
|
||||
show(L);
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
//算法8.4 冒泡排序
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#define MAXSIZE 20 //顺序表的最大长度
|
||||
typedef struct
|
||||
{
|
||||
int key;
|
||||
char *otherinfo;
|
||||
}ElemType;
|
||||
//顺序表的存储结构
|
||||
typedef struct
|
||||
{
|
||||
ElemType *r; //存储空间的基地址
|
||||
int length; //顺序表长度
|
||||
}SqList; //顺序表类型
|
||||
|
||||
void BubbleSort(SqList &L)
|
||||
{
|
||||
//对顺序表L做冒泡排序
|
||||
int m,j,flag;
|
||||
ElemType t;
|
||||
m=L.length-1; flag=1; //flag用来标记某一趟排序是否发生交换
|
||||
while((m>0)&&(flag==1))
|
||||
{
|
||||
flag=0; //flag置为0,如果本趟排序没有发生交换,则不会执行下一趟排序
|
||||
for(j=1;j<=m;j++)
|
||||
if(L.r[j].key>L.r[j+1].key)
|
||||
{
|
||||
flag=1; //flag置为1,表示本趟排序发生了交换
|
||||
t=L.r[j];L.r[j]=L.r[j+1];L.r[j+1]=t; //交换前后两个记录
|
||||
} //if
|
||||
--m;
|
||||
} //while
|
||||
} //BubbleSort
|
||||
|
||||
void Create_Sq(SqList &L)
|
||||
{
|
||||
int i,n;
|
||||
cout<<"请输入数据个数,不超过"<<MAXSIZE<<"个。"<<endl;
|
||||
cin>>n; //输入个数
|
||||
cout<<"请输入待排序的数据:\n";
|
||||
while(n>MAXSIZE)
|
||||
{
|
||||
cout<<"个数超过上限,不能超过"<<MAXSIZE<<",请重新输入"<<endl;
|
||||
cin>>n;
|
||||
}
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
cin>>L.r[i].key;
|
||||
L.length++;
|
||||
}
|
||||
}
|
||||
|
||||
void show(SqList L)
|
||||
{
|
||||
int i;
|
||||
for(i=1;i<=L.length;i++)
|
||||
cout<<L.r[i].key<<endl;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
SqList L;
|
||||
L.r=new ElemType[MAXSIZE+1];
|
||||
L.length=0;
|
||||
Create_Sq(L);
|
||||
BubbleSort(L);
|
||||
cout<<"排序后的结果为:"<<endl;
|
||||
show(L);
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
//算法8.5 快速排序
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#define MAXSIZE 20 //顺序表的最大长度
|
||||
typedef struct
|
||||
{
|
||||
int key;
|
||||
char *otherinfo;
|
||||
}ElemType;
|
||||
//顺序表的存储结构
|
||||
typedef struct
|
||||
{
|
||||
ElemType *r; //存储空间的基地址
|
||||
int length; //顺序表长度
|
||||
}SqList; //顺序表类型
|
||||
|
||||
|
||||
int Partition(SqList &L,int low,int high)
|
||||
{
|
||||
//对顺序表L中的子表r[low..high]进行一趟排序,返回枢轴位置
|
||||
int pivotkey;
|
||||
L.r[0]=L.r[low]; //用子表的第一个记录做枢轴记录
|
||||
pivotkey=L.r[low].key; //枢轴记录关键字保存在pivotkey中
|
||||
while(low<high)
|
||||
{ //从表的两端交替地向中间扫描
|
||||
while(low<high&&L.r[high].key>=pivotkey) --high;
|
||||
L.r[low]=L.r[high]; //将比枢轴记录小的记录移到低端
|
||||
while(low<high&&L.r[low].key<=pivotkey) ++low;
|
||||
L.r[high]=L.r[low]; //将比枢轴记录大的记录移到高端
|
||||
}//while
|
||||
L.r[low]=L.r[0]; //枢轴记录到位
|
||||
return low; //返回枢轴位置
|
||||
}//Partition
|
||||
|
||||
void QSort(SqList &L,int low,int high)
|
||||
{ //调用前置初值:low=1; high=L.length;
|
||||
//对顺序表L中的子序列L.r[low..high]做快速排序
|
||||
int pivotloc;
|
||||
if(low<high)
|
||||
{ //长度大于1
|
||||
pivotloc=Partition(L,low,high); //将L.r[low..high]一分为二,pivotloc是枢轴位置
|
||||
QSort(L,low,pivotloc-1); //对左子表递归排序
|
||||
QSort(L,pivotloc+1,high); //对右子表递归排序
|
||||
}
|
||||
} //QSort
|
||||
|
||||
void QuickSort(SqList &L)
|
||||
{
|
||||
//对顺序表L做快速排序
|
||||
QSort(L,1,L.length);
|
||||
} //QuickSort
|
||||
|
||||
void Create_Sq(SqList &L)
|
||||
{
|
||||
int i,n;
|
||||
cout<<"请输入数据个数,不超过"<<MAXSIZE<<"个。"<<endl;
|
||||
cin>>n; //输入个数
|
||||
cout<<"请输入待排序的数据:\n";
|
||||
while(n>MAXSIZE)
|
||||
{
|
||||
cout<<"个数超过上限,不能超过"<<MAXSIZE<<",请重新输入"<<endl;
|
||||
cin>>n;
|
||||
}
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
cin>>L.r[i].key;
|
||||
L.length++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void show(SqList L)
|
||||
{
|
||||
int i;
|
||||
for(i=1;i<=L.length;i++)
|
||||
cout<<L.r[i].key<<endl;
|
||||
}
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
SqList L;
|
||||
L.r=new ElemType[MAXSIZE+1];
|
||||
L.length=0;
|
||||
Create_Sq(L);
|
||||
QuickSort(L);
|
||||
cout<<"排序后的结果为:"<<endl;
|
||||
show(L);
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
//算法8.6 简单选择排序
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#define MAXSIZE 20 //顺序表的最大长度
|
||||
typedef struct
|
||||
{
|
||||
int key;
|
||||
char *otherinfo;
|
||||
}ElemType;
|
||||
//顺序表的存储结构
|
||||
typedef struct
|
||||
{
|
||||
ElemType *r; //存储空间的基地址
|
||||
int length; //顺序表长度
|
||||
}SqList; //顺序表类型
|
||||
|
||||
void SelectSort(SqList &L)
|
||||
{
|
||||
//对顺序表L做简单选择排序
|
||||
int i,j,k;
|
||||
ElemType t;
|
||||
for(i=1;i<L.length;++i)
|
||||
{ //在L.r[i..L.length] 中选择关键字最小的记录
|
||||
k=i;
|
||||
for(j=i+1;j<=L.length;++j)
|
||||
if(L.r[j].key<L.r[k].key) k=j; //k指向此趟排序中关键字最小的记录
|
||||
if(k!=i) {t=L.r[i];L.r[i]=L.r[k];L.r[k]=t;} //交换r[i]与r[k]
|
||||
} //for
|
||||
} // SelectSort
|
||||
|
||||
void Create_Sq(SqList &L)
|
||||
{
|
||||
int i,n;
|
||||
cout<<"请输入数据个数,不超过"<<MAXSIZE<<"个。"<<endl;
|
||||
cin>>n; //输入个数
|
||||
cout<<"请输入待排序的数据:\n";
|
||||
while(n>MAXSIZE)
|
||||
{
|
||||
cout<<"个数超过上限,不能超过"<<MAXSIZE<<",请重新输入"<<endl;
|
||||
cin>>n;
|
||||
}
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
cin>>L.r[i].key;
|
||||
L.length++;
|
||||
}
|
||||
}
|
||||
|
||||
void show(SqList L)
|
||||
{
|
||||
int i;
|
||||
for(i=1;i<=L.length;i++)
|
||||
cout<<L.r[i].key<<endl;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
SqList L;
|
||||
L.r=new ElemType[MAXSIZE+1];
|
||||
L.length=0;
|
||||
Create_Sq(L);
|
||||
SelectSort(L);
|
||||
cout<<"排序后的结果为:"<<endl;
|
||||
show(L);
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
//算法8.7 筛选法调整堆
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#define MAXSIZE 20 //顺序表的最大长度
|
||||
typedef struct
|
||||
{
|
||||
int key;
|
||||
char *otherinfo;
|
||||
}ElemType;
|
||||
//顺序表的存储结构
|
||||
typedef struct
|
||||
{
|
||||
ElemType *r; //存储空间的基地址
|
||||
int length; //顺序表长度
|
||||
}SqList; //顺序表类型
|
||||
|
||||
void HeapAdjust(SqList &L,int s,int m)
|
||||
{
|
||||
//假设r[s+1..m]已经是堆,将r[s..m]调整为以r[s]为根的大根堆
|
||||
ElemType rc;
|
||||
int j;
|
||||
rc=L.r[s];
|
||||
for(j=2*s;j<=m;j*=2)
|
||||
{ //沿key较大的孩子结点向下筛选
|
||||
if(j<m&&L.r[j].key<L.r[j+1].key) ++j; //j为key较大的记录的下标
|
||||
if(rc.key>=L.r[j].key) break; //rc应插入在位置s上
|
||||
L.r[s]=L.r[j]; s=j;
|
||||
}
|
||||
L.r[s]=rc; //插入
|
||||
} //HeapAdjust
|
||||
|
||||
void Create_Sq(SqList &L)
|
||||
{
|
||||
int i,n;
|
||||
cout<<"请输入数据个数,不超过"<<MAXSIZE<<"个。"<<endl;
|
||||
cin>>n; //输入个数
|
||||
cout<<"请输入待调整的数据:\n";
|
||||
while(n>MAXSIZE)
|
||||
{
|
||||
cout<<"个数超过上限,不能超过"<<MAXSIZE<<",请重新输入"<<endl;
|
||||
cin>>n;
|
||||
}
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
cin>>L.r[i].key;
|
||||
L.length++;
|
||||
}
|
||||
}
|
||||
void show(SqList L)
|
||||
{
|
||||
int i;
|
||||
for(i=1;i<=L.length;i++)
|
||||
cout<<L.r[i].key<<endl;
|
||||
}
|
||||
void main()
|
||||
{
|
||||
SqList L;
|
||||
L.r=new ElemType[MAXSIZE+1];
|
||||
L.length=0;
|
||||
Create_Sq(L);
|
||||
HeapAdjust(L,1,L.length);
|
||||
cout<<"调整后的结果为:"<<endl;
|
||||
show(L);
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
//算法8.8 建初堆
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#define MAXSIZE 20 //顺序表的最大长度
|
||||
typedef struct
|
||||
{
|
||||
int key;
|
||||
char *otherinfo;
|
||||
}ElemType;
|
||||
//顺序表的存储结构
|
||||
typedef struct
|
||||
{
|
||||
ElemType *r; //存储空间的基地址
|
||||
int length; //顺序表长度
|
||||
}SqList; //顺序表类型
|
||||
|
||||
//用算法8.7 筛选法调整堆
|
||||
void HeapAdjust(SqList &L,int s,int m)
|
||||
{
|
||||
//假设r[s+1..m]已经是堆,将r[s..m]调整为以r[s]为根的大根堆
|
||||
ElemType rc;
|
||||
int j;
|
||||
rc=L.r[s];
|
||||
for(j=2*s;j<=m;j*=2)
|
||||
{ //沿key较大的孩子结点向下筛选
|
||||
if(j<m&&L.r[j].key<L.r[j+1].key) ++j; //j为key较大的记录的下标
|
||||
if(rc.key>=L.r[j].key) break; //rc应插入在位置s上
|
||||
L.r[s]=L.r[j]; s=j;
|
||||
}
|
||||
L.r[s]=rc; //插入
|
||||
} //HeapAdjust
|
||||
|
||||
void Create_Sq(SqList &L)
|
||||
{
|
||||
int i,n;
|
||||
cout<<"请输入数据个数,不超过"<<MAXSIZE<<"个。"<<endl;
|
||||
cin>>n; //输入个数
|
||||
cout<<"请输入数据:\n";
|
||||
while(n>MAXSIZE)
|
||||
{
|
||||
cout<<"个数超过上限,不能超过"<<MAXSIZE<<",请重新输入"<<endl;
|
||||
cin>>n;
|
||||
}
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
cin>>L.r[i].key;
|
||||
L.length++;
|
||||
}
|
||||
}
|
||||
|
||||
void CreatHeap(SqList &L)
|
||||
{
|
||||
//把无序序列L.r[1..n]建成大根堆
|
||||
int i,n;
|
||||
n=L.length;
|
||||
for(i=n/2;i>0;--i) //反复调用HeapAdjust
|
||||
HeapAdjust(L,i,n);
|
||||
} //CreatHeap
|
||||
void show(SqList L)
|
||||
{
|
||||
int i;
|
||||
for(i=1;i<=L.length;i++)
|
||||
cout<<L.r[i].key<<endl;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
SqList L;
|
||||
L.r=new ElemType[MAXSIZE+1];
|
||||
L.length=0;
|
||||
Create_Sq(L);
|
||||
CreatHeap(L);
|
||||
cout<<"建立成堆的序列为:"<<endl;
|
||||
show(L);
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
//算法8.9 堆排序
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#define MAXSIZE 20 //顺序表的最大长度
|
||||
typedef struct
|
||||
{
|
||||
int key;
|
||||
char *otherinfo;
|
||||
}ElemType;
|
||||
//顺序表的存储结构
|
||||
typedef struct
|
||||
{
|
||||
ElemType *r; //存储空间的基地址
|
||||
int length; //顺序表长度
|
||||
}SqList; //顺序表类型
|
||||
|
||||
//用算法8.7 筛选法调整堆
|
||||
void HeapAdjust(SqList &L,int s,int m)
|
||||
{
|
||||
//假设r[s+1..m]已经是堆,将r[s..m]调整为以r[s]为根的大根堆
|
||||
ElemType rc;
|
||||
int j;
|
||||
rc=L.r[s];
|
||||
for(j=2*s;j<=m;j*=2)
|
||||
{ //沿key较大的孩子结点向下筛选
|
||||
if(j<m&&L.r[j].key<L.r[j+1].key) ++j; //j为key较大的记录的下标
|
||||
if(rc.key>=L.r[j].key) break; //rc应插入在位置s上
|
||||
L.r[s]=L.r[j]; s=j;
|
||||
}
|
||||
L.r[s]=rc; //插入
|
||||
}
|
||||
//HeapAdjust
|
||||
void Create_Sq(SqList &L)
|
||||
{
|
||||
int i,n;
|
||||
cout<<"请输入数据个数,不超过"<<MAXSIZE<<"个。"<<endl;
|
||||
cin>>n; //输入个数
|
||||
cout<<"请输入待排序的数据:\n";
|
||||
while(n>MAXSIZE)
|
||||
{
|
||||
cout<<"个数超过上限,不能超过"<<MAXSIZE<<",请重新输入"<<endl;
|
||||
cin>>n;
|
||||
}
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
cin>>L.r[i].key;
|
||||
L.length++;
|
||||
}
|
||||
}
|
||||
|
||||
//用算法8.8 建初堆
|
||||
void CreatHeap(SqList &L)
|
||||
{
|
||||
//把无序序列L.r[1..n]建成大根堆
|
||||
int i,n;
|
||||
n=L.length;
|
||||
for(i=n/2;i>0;--i) //反复调用HeapAdjust
|
||||
HeapAdjust(L,i,n);
|
||||
} //CreatHeap
|
||||
|
||||
void HeapSort(SqList &L)
|
||||
{
|
||||
//对顺序表L进行堆排序
|
||||
int i;
|
||||
ElemType x;
|
||||
CreatHeap(L); //把无序序列L.r[1..L.length]建成大根堆
|
||||
for(i=L.length;i>1;--i)
|
||||
{
|
||||
x=L.r[1]; //将堆顶记录和当前未经排序子序列L.r[1..i]中最后一个记录互换
|
||||
L.r[1]=L.r[i];
|
||||
L.r[i]=x;
|
||||
HeapAdjust(L,1,i-1); //将L.r[1..i-1]重新调整为大根堆
|
||||
}//for
|
||||
}//HeapSort
|
||||
void show(SqList L)
|
||||
{
|
||||
int i;
|
||||
for(i=1;i<=L.length;i++)
|
||||
cout<<L.r[i].key<<endl;
|
||||
}
|
||||
void main()
|
||||
{
|
||||
SqList L;
|
||||
L.r=new ElemType[MAXSIZE+1];
|
||||
L.length=0;
|
||||
Create_Sq(L);
|
||||
HeapSort(L);
|
||||
cout<<"排序后的结果为:"<<endl;
|
||||
show(L);
|
||||
}
|
||||
Reference in new issue
Block a user