第十五周 【项目1
来源:互联网 发布:网络系统集成试题 编辑:程序博客网 时间:2024/06/06 02:45
用序列{57, 40, 38, 11, 13, 34, 48, 75, 6, 19, 9, 7}作为测试数据,运行并本周视频中所讲过的算法对应 程序,观察运行结果并深刻领会算法的思路和实现方法:(1)直接插入排序;(2)希尔排序;(3)冒泡排序;(4)快速排序;(5)直接选择排序;(6)堆排序;(7)归并排序;(8)基数排序。
(1)直接插入排序
//简单选择排序void SelectSort(RecType R[],int n){ int i,j,k,l; RecType temp; for(i=0;i<n-1;i++) { k=i; for(j=i+1;j<n;j++) if(R[j].key>R[k].key) k=j; if(k!=i) { temp=R[i]; R[i]=R[k]; R[k]=temp; } }}
1.直接插入排序
#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void InsertSort(RecType R[],int n) //对R[0..n-1]按递增有序进行直接插入排序{ int i,j; RecType tmp; for (i=1; i<n; i++) { tmp=R[i]; j=i-1; //从右向左在有序区R[0..i-1]中找R[i]的插入位置 while (j>=0 && tmp.key<R[j].key) { R[j+1]=R[j]; //将关键字大于R[i].key的记录后移 j--; } R[j+1]=tmp; //在j+1处插入R[i] }}int main(){ int i,n=10; RecType R[MaxSize]; KeyType a[]= {9,8,7,6,5,4,3,2,1,0}; for (i=0; i<n; i++) R[i].key=a[i]; printf("排序前:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); InsertSort(R,n); printf("排序后:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
2.显示直接插入排序过程
#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void InsertSort(RecType R[],int n) //对R[0..n-1]按递增有序进行直接插入排序{ int i,j,k; RecType tmp; for (i=1; i<n; i++) { tmp=R[i]; j=i-1; //从右向左在有序区R[0..i-1]中找R[i]的插入位置 while (j>=0 && tmp.key<R[j].key) { R[j+1]=R[j]; //将关键字大于R[i].key的记录后移 j--; } R[j+1]=tmp; //在j+1处插入R[i] printf("i=%d: ",i); for (k=0; k<n; k++) printf("%d ",R[k].key); printf("\n"); }}int main(){ int i,n=10; RecType R[MaxSize]; KeyType a[]= {9,8,7,6,5,4,3,2,1,0}; for (i=0; i<n; i++) R[i].key=a[i]; printf("排序前:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); InsertSort(R,n); printf("排序后:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
3.折半插入排序
#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void InsertSort1(RecType R[],int n) //对R[0..n-1]按递增有序进行直接插入排序{ int i,j,low,high,mid; RecType tmp; for (i=1; i<n; i++) { tmp=R[i]; low=0; high=i-1; while (low<=high) { mid=(low+high)/2; if (tmp.key<R[mid].key) high=mid-1; else low=mid+1; } for (j=i-1; j>=high+1; j--) R[j+1]=R[j]; R[high+1]=tmp; }}int main(){ int i,n=10; RecType R[MaxSize]; KeyType a[]= {9,8,7,6,5,4,3,2,1,0}; for (i=0; i<n; i++) R[i].key=a[i]; printf("排序前:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); InsertSort1(R,n); printf("排序后:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); return 0;}
(2)希尔排序1.希尔排序
#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void ShellSort(RecType R[],int n) //希尔排序算法{ int i,j,gap; RecType tmp; gap=n/2; //增量置初值 while (gap>0) { for (i=gap; i<n; i++) //对所有相隔gap位置的所有元素组进行排序 { tmp=R[i]; j=i-gap; while (j>=0 && tmp.key<R[j].key)//对相隔gap位置的元素组进行排序 { R[j+gap]=R[j]; j=j-gap; } R[j+gap]=tmp; j=j-gap; } gap=gap/2; //减小增量 }}int main(){ int i,n=11; RecType R[MaxSize]; KeyType a[]= {16,25,12,30,47,11,23,36,9,18,31}; for (i=0; i<n; i++) R[i].key=a[i]; printf("排序前:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); ShellSort(R,n); printf("排序后:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
2.排序中输出每一趟的中间结果
#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void ShellSort(RecType R[],int n) //希尔排序算法{ int i,j,gap,k; RecType tmp; gap=n/2; //增量置初值 while (gap>0) { for (i=gap; i<n; i++) //对所有相隔gap位置的所有元素组进行排序 { tmp=R[i]; j=i-gap; while (j>=0 && tmp.key<R[j].key)//对相隔gap位置的元素组进行排序 { R[j+gap]=R[j]; j=j-gap; } R[j+gap]=tmp; j=j-gap; } printf("gap=%d:",gap); for (k=0; k<n; k++) printf("%d ",R[k].key); printf("\n"); gap=gap/2; //减小增量 }}int main(){ int i,n=11; RecType R[MaxSize]; KeyType a[]= {16,25,12,30,47,11,23,36,9,18,31}; for (i=0; i<n; i++) R[i].key=a[i]; printf("排序前:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); ShellSort(R,n); printf("排序后:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); return 0;}
(3)冒泡排序1.冒泡排序
#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void BubbleSort(RecType R[],int n){ int i,j,k; RecType tmp; for (i=0; i<n-1; i++) { for (j=n-1; j>i; j--) //比较,找出本趟最小关键字的记录 if (R[j].key<R[j-1].key) { tmp=R[j]; //R[j]与R[j-1]进行交换,将最小关键字记录前移 R[j]=R[j-1]; R[j-1]=tmp; } printf("i=%d: ",i); for (k=0; k<n; k++) printf("%d ",R[k].key); printf("\n"); }}int main(){ int i,n=10; RecType R[MaxSize]; KeyType a[]= {9,8,7,6,5,4,3,2,1,0}; for (i=0; i<n; i++) R[i].key=a[i]; printf("排序前:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); BubbleSort(R,n); printf("排序后:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
2.改进的算法(一趟冒泡没有交换,即时结束排序过程)
#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void BubbleSort1(RecType R[],int n){ int i,j,k,exchange; RecType tmp; for (i=0; i<n-1; i++) { exchange=0; for (j=n-1; j>i; j--) //比较,找出最小关键字的记录 if (R[j].key<R[j-1].key) { tmp=R[j]; //R[j]与R[j-1]进行交换,将最小关键字记录前移 R[j]=R[j-1]; R[j-1]=tmp; exchange=1; } printf("i=%d: ",i); for (k=0; k<n; k++) printf("%d ",R[k].key); printf("\n"); if (exchange==0) //中途结束算法 return; }}int main(){ int i,n=10; RecType R[MaxSize]; KeyType a[]= {0,1,7,2,5,4,3,6,8,9}; for (i=0; i<n; i++) R[i].key=a[i]; printf("排序前:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); BubbleSort1(R,n); printf("排序后:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); return 0;}
(4)快速排序1.以第1个元素作为基准
#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void QuickSort(RecType R[],int s,int t) //对R[s]至R[t]的元素进行快速排序{ int i=s,j=t; RecType tmp; if (s<t) //区间内至少存在两个元素的情况 { tmp=R[s]; //用区间的第1个记录作为基准 while (i!=j) //从区间两端交替向中间扫描,直至i=j为止 { while (j>i && R[j].key>=tmp.key) j--; //从右向左扫描,找第1个小于tmp.key的R[j] R[i]=R[j]; //找到这样的R[j],R[i]"R[j]交换 while (i<j && R[i].key<=tmp.key) i++; //从左向右扫描,找第1个大于tmp.key的记录R[i] R[j]=R[i]; //找到这样的R[i],R[i]"R[j]交换 } R[i]=tmp; QuickSort(R,s,i-1); //对左区间递归排序 QuickSort(R,i+1,t); //对右区间递归排序 }}int main(){ int i,n=10; RecType R[MaxSize]; KeyType a[]= {6,8,7,9,0,1,3,2,4,5}; for (i=0; i<n; i++) R[i].key=a[i]; printf("排序前:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); QuickSort(R,0,n-1); printf("排序后:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
2.以中间位置的元素作为基准
#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void QuickSort1(RecType R[],int s,int t) //对R[s]至R[t]的元素进行快速排序{ int i=s,j=t; KeyType pivot; RecType tmp; pivot = R[(s+t)/2].key; //用区间的中间位置的元素作为关键字 if (s<t) //区间内至少存在两个元素的情况 { while (i!=j) //从区间两端交替向中间扫描,直至i=j为止 { while (j>i && R[j].key>pivot) j--; //从右向左扫描,找第1个小于基准的R[j] while (i<j && R[i].key<pivot) i++; //从左向右扫描,找第1个大于基准记录R[i] if(i<j) //将前后的两个失序元素进行交换 { tmp=R[i]; R[i]=R[j]; R[j]=tmp; } } QuickSort1(R,s,i-1); //对左区间递归排序 QuickSort1(R,j+1,t); //对右区间递归排序 }}int main(){ int i,n=10; RecType R[MaxSize]; KeyType a[]= {6,8,7,9,0,1,3,2,4,5}; for (i=0; i<n; i++) R[i].key=a[i]; printf("排序前:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); QuickSort1(R,0,n-1); printf("排序后:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); return 0;}
(6)堆排序
1.堆排序
#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义//调整堆void sift(RecType R[],int low,int high){ int i=low,j=2*i; //R[j]是R[i]的左孩子 RecType temp=R[i]; while (j<=high) { if (j<high && R[j].key<R[j+1].key) //若右孩子较大,把j指向右孩子 j++; //变为2i+1 if (temp.key<R[j].key) { R[i]=R[j]; //将R[j]调整到双亲结点位置上 i=j; //修改i和j值,以便继续向下筛选 j=2*i; } else break; //筛选结束 } R[i]=temp; //被筛选结点的值放入最终位置}//堆排序void HeapSort(RecType R[],int n){ int i; RecType temp; for (i=n/2; i>=1; i--) //循环建立初始堆 sift(R,i,n); for (i=n; i>=2; i--) //进行n-1次循环,完成推排序 { temp=R[1]; //将第一个元素同当前区间内R[1]对换 R[1]=R[i]; R[i]=temp; sift(R,1,i-1); //筛选R[1]结点,得到i-1个结点的堆 }}int main(){ int i,n=10; RecType R[MaxSize]; KeyType a[]= {0,6,8,7,9,0,1,3,2,4,5};//a[0]空闲,不作为关键字 for (i=1; i<=n; i++) R[i].key=a[i]; printf("排序前:"); for (i=1; i<=n; i++) printf("%d ",R[i].key); printf("\n"); HeapSort(R,n); printf("排序后:"); for (i=1; i<=n; i++) printf("%d ",R[i].key); printf("\n"); return 0;}
2.
例:对{57, 40, 38, 11, 13, 34, 48, 75, 6, 19, 9, 7}进行堆排序的过程。
算法如下:
void HeapSort(RecType R[],int n){ int i; RecType temp; //(1)循环建立初始堆 for (i=n/2; i>=1; i--) sift(R,i,n); //(2)进行n-1次循环,完成推排序 for (i=n; i>=2; i--) { temp=R[1]; //将第一个元素同当前区间内R[1]对换 R[1]=R[i]; R[i]=temp; sift(R,1,i-1); //筛选R[1]结点,得到i-1个结点的堆 }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
(1)循环建立初始堆
for (i=n/2; i>=1; i--) sift(R,i,n);
- 1
- 2
用给出的序列构造堆的初始状态如下:
在此基础上,根据上述代码,从最后一个分支节点开始调整,目标是得到大根堆。过程如下图:
这个堆的存储结构是:
(2)进行n-1次循环,完成推排序
for (i=n; i>=2; i--) { temp=R[1]; //最大值交换到最后 R[1]=R[i]; R[i]=temp; sift(R,1,i-1); //前面的无序区调整为堆 }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
过程图示如下:
(7)归并排序
1.归并排序
#include <stdio.h>#include <malloc.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void Merge(RecType R[],int low,int mid,int high){ RecType *R1; int i=low,j=mid+1,k=0; //k是R1的下标,i、j分别为第1、2段的下标 R1=(RecType *)malloc((high-low+1)*sizeof(RecType)); //动态分配空间 while (i<=mid && j<=high) //在第1段和第2段均未扫描完时循环 if (R[i].key<=R[j].key) //将第1段中的记录放入R1中 { R1[k]=R[i]; i++; k++; } else //将第2段中的记录放入R1中 { R1[k]=R[j]; j++; k++; } while (i<=mid) //将第1段余下部分复制到R1 { R1[k]=R[i]; i++; k++; } while (j<=high) //将第2段余下部分复制到R1 { R1[k]=R[j]; j++; k++; } for (k=0,i=low; i<=high; k++,i++) //将R1复制回R中 R[i]=R1[k];}void MergePass(RecType R[],int length,int n) //对整个数序进行一趟归并{ int i; for (i=0; i+2*length-1<n; i=i+2*length) //归并length长的两相邻子表 Merge(R,i,i+length-1,i+2*length-1); if (i+length-1<n) //余下两个子表,后者长度小于length Merge(R,i,i+length-1,n-1); //归并这两个子表}void MergeSort(RecType R[],int n) //自底向上的二路归并算法{ int length; for (length=1; length<n; length=2*length) //进行log2n趟归并 MergePass(R,length,n);}int main(){ int i,n=10; RecType R[MaxSize]; KeyType a[]= {9,8,7,6,5,4,3,2,1,0}; for (i=0; i<n; i++) R[i].key=a[i]; printf("排序前:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); MergeSort(R,n); printf("排序后:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); return 0;}
#include <stdio.h>#include <malloc.h>#include <string.h>#define MAXE 20 //线性表中最多元素个数#define MAXR 10 //基数的最大取值#define MAXD 8 //关键字位数的最大取值typedef struct node{ char data[MAXD]; //记录的关键字定义的字符串 struct node *next;} RecType;void CreaLink(RecType *&p,char *a[],int n);void DispLink(RecType *p);void RadixSort(RecType *&p,int r,int d) //实现基数排序:*p为待排序序列链表指针,r为基数,d为关键字位数{ RecType *head[MAXR],*tail[MAXR],*t; //定义各链队的首尾指针 int i,j,k; for (i=0; i<=d-1; i++) //从低位到高位循环 { for (j=0; j<r; j++) //初始化各链队首、尾指针 head[j]=tail[j]=NULL; while (p!=NULL) //对于原链表中每个结点循环 { k=p->data[i]-'0'; //找第k个链队 if (head[k]==NULL) //进行分配 { head[k]=p; tail[k]=p; } else { tail[k]->next=p; tail[k]=p; } p=p->next; //取下一个待排序的元素 } p=NULL; //重新用p来收集所有结点 for (j=0; j<r; j++) //对于每一个链队循环 if (head[j]!=NULL) //进行收集 { if (p==NULL) { p=head[j]; t=tail[j]; } else { t->next=head[j]; t=tail[j]; } } t->next=NULL; //最后一个结点的next域置NULL //以下的显示并非必要 printf(" 按%d位排序\t",i); DispLink(p); }}void CreateLink(RecType *&p,char a[MAXE][MAXD],int n) //采用后插法产生链表{ int i; RecType *s,*t; for (i=0; i<n; i++) { s=(RecType *)malloc(sizeof(RecType)); strcpy(s->data,a[i]); if (i==0) { p=s; t=s; } else { t->next=s; t=s; } } t->next=NULL;}void DispLink(RecType *p) //输出链表{ while (p!=NULL) { printf("%c%c ",p->data[1],p->data[0]); p=p->next; } printf("\n");}int main(){ int n=10,r=10,d=2; int i,j,k; RecType *p; char a[MAXE][MAXD]; int b[]= {75,23,98,44,57,12,29,64,38,82}; for (i=0; i<n; i++) //将b[i]转换成字符串 { k=b[i]; for (j=0; j<d; j++) //例如b[0]=75,转换后a[0][0]='7',a[0][1]='5' { a[i][j]=k%10+'0'; k=k/10; } a[i][j]='\0'; } CreateLink(p,a,n); printf("\n"); printf(" 初始关键字\t"); //输出初始关键字序列 DispLink(p); RadixSort(p,10,2); printf(" 最终结果\t"); //输出最终结果 DispLink(p); printf("\n"); return 0;}
阅读全文
0 0
- 第十五周 【项目1
- 第十五周项目1
- 第十五周项目1
- 第十五周项目1
- 第十五周项目1
- 第十五周项目1
- 第十五周项目1
- 第十五周项目2-1
- 第十五周阅读项目1
- 第十五周 项目1 基数排序
- 第十五周项目1 哈希表
- 第十五周项目一-工资项目1
- 第十五周项目1 折腾二维数组
- 第十五周上机任务-项目1
- 第十五周 项目1--折腾二维数组
- 第十五周项目1-折腾二维数组
- 第十五周项目1--工资排序
- 第十五周上机项目3--(1)
- Air Raid(最小路径覆盖)
- 【机器学习】常用算法PK
- linux常用命令(5)——压缩、查看内存、RPM、YUM
- 数组绑定到dataset上
- auc指标含义的理解
- 第十五周 【项目1
- jvm 内存划分 栈区 堆区 方法区
- 一个国外系统且严谨的WEB开发学习网站
- Two Sum
- Vmware虚拟机三种网络模式详解
- 项目中学习的知识(一)
- 第三周项目一
- VS 如何设置编译后的程序可以以管理员身份运行
- java进制转换