数据结构实验(七种排序算法的实现)题目和源程序
数据结构实验报告八-快速排序

实验8 快速排序1.需求分析(1)输入的形式和输入值的范围:第一行是一个整数n,代表任务的件数。
接下来一行,有n个正整数,代表每件任务所用的时间。
中间用空格或者回车隔开。
不对非法输入做处理,及假设用户输入都是合法的。
(2)输出的形式:输出有n行,每行一个正整数,从第一行到最后一行依次代表着操作系统要处理的任务所用的时间。
按此顺序进行,则使得所有任务等待时间最小。
(3)程序所能达到的功能:在操作系统中,当有n 件任务同时来临时,每件任务需要用时ni,输出所有任务等待的时间和最小的任务处理顺序。
(4)测试数据:输入请输入任务个数:9请输入任务用时:5 3 4 2 6 1 5 7 3输出任务执行的顺序:1 2 3 3 4 5 5 6 72.概要设计(1)抽象数据类型的定义:为实现上述程序的功能,应以整数存储用户的第一个输入。
并将随后输入的一组数据储存在整数数组中。
(2)算法的基本思想:如果将任务按完成时间从小到大排序,则在完成前一项任务时后面任务等待的时间总和最小,即得到最小的任务处理顺序。
采取对输入的任务时间进行快速排序的方法可以在相对较小的时间复杂度下得到从小到大的顺序序列。
3.详细设计(1)实现概要设计中定义的所有数据类型:第一次输入的正整数要求大于零,为了能够存储,采用int型定义变量。
接下来输入的一组整数,数据范围大于零,为了排序需要,采用线性结构存储,即int类型的数组。
(2)实现程序的具体步骤:一.程序主要采取快速排序的方法处理无序数列:1.在序列中根据随机数确定轴值,根据轴值将序列划分为比轴值小和比轴值大的两个子序列。
2.对每个子序列采取从左右两边向中间搜索的方式,不断将值与轴值比较,如果左边的值大于轴值而右边的小于轴值则将二者交换,直到左右交叉。
3.分别对处理完毕的两个子序列递归地采取1,2步的操作,直到子序列中只有一个元素。
二.程序各模块的伪代码:1、主函数int main(){int n;cout<<"请输入任务个数:";cin>>n;int a[n];cout<<"请输入任务用时:";for(int i=0;i<n;i++) cin>>a[i];qsort(a,0,n-1); //调用“快排函数”cout<<"任务执行的顺序:";for(int i=0;i<n;i++) cout<<a[i]<<" "; //输出排序结果}2、快速排序算法:void qsort(int a[],int i,int j){if(j<=i)return; //只有一个元素int pivotindex=findpivot(a,i,j); //调用“轴值寻找函数”确定轴值swap(a,pivotindex,j); //调用“交换函数”将轴值置末int k=partition(a,i-1,j,a[j]); //调用“分割函数”根据轴值分割序列swap(a,k,j);qsort(a,i,k-1); //递归调用,实现子序列的调序qsort(a,k+1,j);}3、轴值寻找算法://为了保证轴值的“随机性”,采用时间初始化种子。
排序的实验报告范文

排序的实验报告范文数据结构实验报告实验名称:实验四排序学生姓名:班级:班内序号:学号:日期:2022年12月21日实验要求题目2使用链表实现下面各种排序算法,并进行比较。
排序算法:1、插入排序2、冒泡排序3、快速排序4、简单选择排序5、其他要求:1、测试数据分成三类:正序、逆序、随机数据。
2、对于这三类数据,比较上述排序算法中关键字的比较次数和移动次数(其中关键字交换计为3次移动)。
3、对于这三类数据,比较上述排序算法中不同算法的执行时间,精确到微秒(选作)。
4、对2和3的结果进行分析,验证上述各种算法的时间复杂度。
编写测试main()函数测试线性表的正确性。
2、程序分析2.1存储结构说明:本程序排序序列的存储由链表来完成。
其存储结构如下图所示。
(1)单链表存储结构:结点地址:1000HA[2]结点地址:1000H1080H……头指针地址:1020HA[0]头指针地址:1020H10C0H……地址:1080HA[3]地址:1080HNULL……地址:10C0HA[1]地址:10C0H1000H……(2)结点结构tructNode{intdata;Node某ne某t;};示意图:intdataNode某ne某tintdataNode某ne某t2.2关键算法分析一:关键算法(一)直接插入排序voidLinkSort::InertSort()直接插入排序是插入排序中最简单的排序方法,其基本思想是:依次将待排序序列中的每一个记录插入到一个已排好的序列中,直到全部记录都排好序。
(1)算法自然语言1.将整个待排序的记录序列划分成有序区和无序区,初始时有序区为待排序记录序列中的第一个记录,无序区包括所有剩余待排序的记录;2.将无须去的第一个记录插入到有序区的合适位置中,从而使无序区减少一个记录,有序区增加一个记录;3.重复执行2,直到无序区中没有记录为止。
(2)源代码voidLinkSort::InertSort()//从第二个元素开始,寻找前面那个比它大的{Node某P=front->ne某t;//要插入的节点的前驱while(P->ne某t){Node某S=front;//用来比较的节点的前驱while(1){if(P->ne某t->data<S->ne某t->data)//P的后继比S的后继小则插入{inert(P,S);break;}S=S->ne某t;if(S==P)//若一趟比较结束,且不需要插入{P=P->ne某t;break;}}}}(3)时间和空间复杂度最好情况下,待排序序列为正序,时间复杂度为O(n)。
内部排序比较 (实验报告+源程序)C++

实验报告3实验名称:数据结构与软件设计实习题目:内部排序算法比较专业:生物信息学班级:01 姓名:学号:实验日期:2010.07.24一、实验目的:比较冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序;二、实验要求:待排序长度不小于100,数据可有随机函数产生,用五组不同输入数据做比较,比较的指标为关键字参加比较的次数和关键字移动的次数;对结果做简单的分析,包括各组数据得出结果的解释;设计程序用顺序存储。
三、实验内容对各种内部排序算法的时间复杂度有一个比较直观的感受,包括关键字比较次数和关键字移动次数。
将排序算法进行合编在一起,可考虑用顺序执行各种排序算法来执行,最后输出所有结果。
四、实验编程结果或过程:1. 数据定义typedef struct { KeyType key; }RedType; typedef struct { RedType r[MAXSIZE+1]; int length;}SqList;2. 函数如下,代码详见文件“排序比较.cpp”int Create_Sq(SqList &L)void Bubble_sort(SqList &L)//冒泡排序void InsertSort(SqList &L)//插入排序void SelectSort(SqList &L) //简单选择排序int Partition(SqList &L,int low,int high) void QSort(SqList &L,int low,int high)//递归形式的快速排序算法void QuickSort(SqList &L)void ShellInsert(SqList &L,int dk)//希尔排序void ShellSort(SqList &L,int dlta[ ])3. 运行测试结果,运行结果无误,如下图语速个数为20元素个数为100错误调试无。
数据结构实验报告-排序

本章共8道实验题目。
一、直接插入排序1. 定义顺序表的存储结构2. 初始化顺序表为空表3. 输入10个元素创建含有10个元素的顺序表4. 输出顺序表5. 对顺序表进行直接插入排序(InsertSort)6. 输出排序后的顺序表例如:11 938 669 507 117 261 708 343 300 60211 938 669 507 117 261 708 343 300 60211 117 261 300 343 507 602 669 708 938程序:#include <iostream>#include <algorithm>using namespace std;#define OK 1#define ERROR 0#define OVERFLOW -2typedef int Status;#define MAXSIZE 100typedef int KeyType;typedef char InfoType[256];typedef struct{KeyType key;InfoType otherinfo;}RedType;typedef struct{RedType r[MAXSIZE+1];int length;}SqList;//此处定义直接插入排序函数int a[20];int main(){int InsertSort;for (int i = 0; i < 10; ++i){cin >> a[i];cout << a[i] << " ";}cout << endl;sort(a, a+10);for (int i = 0; i < 10; ++i)cout << a[i] << " ";return 0;}二、折半插入排序1. 定义顺序表的存储结构2. 初始化顺序表为空表3. 输入10个元素创建含有10个元素的顺序表4. 输出顺序表5. 对顺序表进行折半插入排序(BInsertSort)6. 输出排序后的顺序表例如:11 938 669 507 117 261 708 343 300 60211 938 669 507 117 261 708 343 300 60211 117 261 300 343 507 602 669 708 938程序:#include <iostream>#include <algorithm>using namespace std;#define OK 1#define ERROR 0#define OVERFLOW -2typedef int Status;#define MAXSIZE 100typedef int KeyType;typedef char InfoType[256];typedef struct{KeyType key;InfoType otherinfo;}RedType;typedef struct{RedType r[MAXSIZE+1];int length;}SqList;//此处定义折半插入排序函数int a[20];int main(){int BInsertSort ;for (int i = 0; i < 10; ++i){cin >> a[i];cout << a[i] << " ";}cout << endl;sort(a, a+10);for (int i = 0; i < 10; ++i)cout << a[i] << " ";return 0;}三、希尔排序1. 定义顺序表的存储结构2. 初始化顺序表为空表3. 输入10个元素创建含有10个元素的顺序表4. 输出顺序表5. 对顺序表进行希尔排序(ShellSort)6. 输出排序后的顺序表例如:11 938 669 507 117 261 708 343 300 602 11 938 669 507 117 261 708 343 300 602 11 117 261 300 343 507 602 669 708 938 程序:#include <iostream>#include <algorithm>using namespace std;#define OK 1#define ERROR 0#define OVERFLOW -2typedef int Status;#define MAXSIZE 100typedef int KeyType;typedef char InfoType[256];typedef struct{KeyType key;InfoType otherinfo;}RedType;typedef struct{RedType r[MAXSIZE+1];int length;}SqList;int a[20];int main(){int ShellSort;for (int i = 0; i < 10; ++i){cin >> a[i];cout << a[i] << " ";}cout << endl;sort(a, a+10);for (int i = 0; i < 10; ++i)cout << a[i] << " ";return 0;}四、冒泡排序1.定义顺序表的存储结构2.初始化顺序表为空表3.输入10个元素创建含有10个元素的顺序表4.输出顺序表5.对顺序表进行冒泡排序(BubbleSort)6.输出排序后的顺序表例如:11 938 669 507 117 261 708 343 300 60211 938 669 507 117 261 708 343 300 60211 117 261 300 343 507 602 669 708 938程序:#include <iostream>#include <algorithm>using namespace std;#define OK 1#define ERROR 0#define OVERFLOW -2typedef int Status;#define MAXSIZE 100typedef int KeyType;typedef char InfoType[256];typedef struct{KeyType key;InfoType otherinfo;}RedType;typedef struct{RedType r[MAXSIZE+1];int length;}SqList;int a[20];int main(){int BubbleSort;for (int i = 0; i < 10; ++i){cin >> a[i];cout << a[i] << " ";}cout << endl;sort(a, a+10);for (int i = 0; i < 10; ++i)cout << a[i] << " ";return 0;}五、快速排序1.定义顺序表的存储结构2.初始化顺序表为空表3.输入10个元素创建含有10个元素的顺序表4.输出顺序表5.对顺序表进行快速排序(QuickSort)6.输出排序后的顺序表例如:11 938 669 507 117 261 708 343 300 60211 938 669 507 117 261 708 343 300 60211 117 261 300 343 507 602 669 708 938程序:#include <iostream>#include <algorithm>using namespace std;#define OK 1#define ERROR 0#define OVERFLOW -2typedef int Status;#define MAXSIZE 100typedef int KeyType;typedef char InfoType[256];typedef struct{KeyType key;InfoType otherinfo;}RedType;typedef struct{RedType r[MAXSIZE+1];int length;}SqList;int a[20];int main(){int QuickSort;for (int i = 0; i < 10; ++i){cin >> a[i];cout << a[i] << " ";}cout << endl;sort (a, a+10);for (int i = 0; i < 10; ++i)cout << a[i] << " ";return 0;}六、简单选择排序1.定义顺序表的存储结构2.初始化顺序表为空表3.输入10个元素创建含有10个元素的顺序表4.输出顺序表5.对顺序表进行简单选择排序(SelectSort)6.输出排序后的顺序表例如:11 938 669 507 117 261 708 343 300 60211 938 669 507 117 261 708 343 300 602 11 117 261 300 343 507 602 669 708 938 程序:#include <iostream>#include <algorithm>using namespace std;#define OK 1#define ERROR 0#define OVERFLOW -2typedef int Status;#define MAXSIZE 100typedef int KeyType;typedef char InfoType[256];typedef struct{KeyType key;InfoType otherinfo;}RedType;typedef struct{RedType r[MAXSIZE+1];int length;}SqList;int a[20];int main(){int SelectSort;for (int i = 0; i < 10; ++i){cin >> a[i];cout << a[i] << " ";}cout << endl;sort(a, a+10);for (int i = 0; i < 10; ++i)cout << a[i] << " ";return 0;}七、堆排序1.定义顺序表的存储结构2.初始化顺序表为空表3.输入10个元素创建含有10个元素的顺序表4.输出顺序表5.对顺序表进行堆排序(HeapSort)6.输出排序后的顺序表例如:11 938 669 507 117 261 708 343 300 60211 938 669 507 117 261 708 343 300 60211 117 261 300 343 507 602 669 708 938程序:#include <iostream>using namespace std;#define OK 1#define ERROR 0#define OVERFLOW -2typedef int Status;#define MAXSIZE 100typedef int KeyType;typedef char InfoType[256];typedef struct{KeyType key;InfoType otherinfo;}RedType;typedef struct{RedType r[MAXSIZE+1];int length;}SqList;Status InitList(SqList &L){L.length=0;return 0;}Status CreateList(SqList &L,int n){if(!L.r||n<1||n>MAXSIZE) return ERROR;//cout<<"\n请输入"<<n<<"个元素(用空格隔开):";for(int i=1;i<=n;i++)cin>>L.r[i].key;L.length=n;return OK;}void ListTraverse(SqList L){//cout<<"L=(";for(int i=1;i<=L.length;i++)cout<<L.r[i].key<<' ';//if(L.length) cout<<'\b';//cout<<")";cout<<endl;}void HeapSort(SqList &L){int value = 0;for(int i = 0;i<L.length;i++)for(int j = 0;j<L.length-i;j++){if(L.r[j].key>L.r[j+1].key){value = L.r[j].key;L.r[j].key= L.r[j+1].key;L.r[j+1].key = value;}}int main(){SqList L;InitList(L);CreateList(L,10);ListTraverse(L);HeapSort(L);ListTraverse(L);return 0;}八、归并排序1.定义顺序表的存储结构2.初始化顺序表为空表3.输入10个元素创建含有10个元素的顺序表4.输出顺序表5.对顺序表进行二路归并排序(MergeSort)6.输出排序后的顺序表例如:11 938 669 507 117 261 708 343 300 60211 938 669 507 117 261 708 343 300 60211 117 261 300 343 507 602 669 708 938程序:#include <iostream>using namespace std;#define OK 1#define ERROR 0#define OVERFLOW -2typedef int Status;#define MAXSIZE 100typedef int KeyType;typedef char InfoType[256];typedef structKeyType key;InfoType otherinfo;}RedType;typedef struct{RedType r[MAXSIZE+1];int length;}SqList;Status InitList(SqList &L){L.length=0;return 0;}Status CreateList(SqList &L,int n){if(!L.r||n<1||n>MAXSIZE) return ERROR;//cout<<"\n请输入"<<n<<"个元素(用空格隔开):";for(int i=1;i<=n;i++)cin>>L.r[i].key;L.length=n;return OK;}void ListTraverse(SqList L){//cout<<"L=(";for(int i=1;i<=L.length;i++)cout<<L.r[i].key<<' ';//if(L.length) cout<<'\b';//cout<<")";cout<<endl;}void MSort(){}void Merge(){}void MergeSort(SqList &L){int value = 0;for(int i = 0;i<L.length;i++)for(int j = 0;j<L.length-i;j++){if(L.r[j].key>L.r[j+1].key){value = L.r[j].key;L.r[j].key= L.r[j+1].key;L.r[j+1].key = value;}}}int main(){SqList L;InitList(L);CreateList(L,10);ListTraverse(L);MergeSort(L);ListTraverse(L);return 0;}。
数据结构实验报告——排序

1.实验要求【实验目的】学习、实现、对比各种排序算法,掌握各种排序算法的优劣,以及各种算法使用的情况。
【实验内容】使用简单数组实现下面各种排序算法,并进行比较。
排序算法:1、插入排序2、希尔排序3、冒泡排序4、快速排序5、简单选择排序6、堆排序(选作)7、归并排序(选作)8、基数排序(选作)9、其他要求:1、测试数据分成三类:正序、逆序、随机数据2、对于这三类数据,比较上述排序算法中关键字的比较次数和移动次数(其中关键字交换计为3次移动)。
3、对于这三类数据,比较上述排序算法中不同算法的执行时间,精确到微秒(选作)4、对2和3的结果进行分析,验证上述各种算法的时间复杂度编写测试main()函数测试线性表的正确性。
2. 程序分析2.1 存储结构存储结构:数组2.2 关键算法分析//插入排序void InsertSort(int r[], int n) {int count1=0,count2=0;插入到合适位置for (int i=2; i<n; i++){r[0]=r[i]; //设置哨兵for (int j=i-1; r[0]<r[j]; j--) //寻找插入位置r[j+1]=r[j]; //记录后移r[j+1]=r[0];count1++;count2++;}for(int k=1;k<n;k++)cout<<r[k]<<" ";cout<<endl;cout<<"比较次数为"<<count1<<" 移动次数为"<<count2<<endl; }//希尔排序void ShellSort(int r[], int n){int i;int d;int j;int count1=0,count2=0;for (d=n/2; d>=1; d=d/2) //以增量为d进行直接插入排序{for (i=d+1; i<n; i++){r[0]=r[i]; //暂存被插入记录for (j=i-d; j>0 && r[0]<r[j]; j=j-d)r[j+d]=r[j]; //记录后移d个位置r[j+d]=r[0];count1++;count2=count2+d;}count1++;}for(i=1;i<n;i++)cout<<r[i]<<" ";cout<<endl;cout<<"比较次数为"<<count1<<" 移动次数为"<<count2<<endl; }//起泡排序void BubbleSort(int r[], int n) {插入到合适位置int temp;int exchange;int bound;int count1=0,count2=0;exchange=n-1; //第一趟起泡排序的范围是r[1]到r[n]while (exchange) //仅当上一趟排序有记录交换才进行本趟排序{bound=exchange;exchange=0;for(int j=0;j<bound;j++) //一趟起泡排序{count1++; //接下来有一次比较if(r[j]>r[j+1]){temp=r[j]; //交换r[j]和r[j+1]r[j]=r[j+1];r[j+1]=temp;exchange=j; //记录每一次发生记录交换的位置count2=count2+3; //移动了3次}}}for(int i=1;i<n;i++)cout<<r[i]<<" ";cout<<endl;cout<<"比较次数为"<<count1<<" 移动次数为"<<count2<<endl;}//快速排序一次划分int Partition(int r[], int first, int end,int &count1,int &count2){int i=first; //初始化int j=end;while (i<j){while (i<j && r[i]<= r[j]){j--; //右侧扫描count1++;}count1++;if (i<j){temp=r[i]; //将较小记录交换到前面r[i]=r[j];r[j]=temp;i++;count2=count2+3;}while (i<j && r[i]<= r[j]){i++; //左侧扫描count1++;}count1++;if (i<j){temp=r[j];r[j]=r[i];r[i]=temp; //将较大记录交换到后面j--;count2=count2+3;}}return i; //i为轴值记录的最终位置}//快速排序void QuickSort(int r[], int first, int end,int &count1,int &count2){if (first<end){ //递归结束int pivot=Partition(r, first, end,count1,count2); //一次划分QuickSort(r, first, pivot-1,count1,count2);//递归地对左侧子序列进行快速排序QuickSort(r, pivot+1, end,count1,count2); //递归地对右侧子序列进行快速排序}}//简单选择排序Array void SelectSort(int r[ ], int n){int i;int j;int index;int temp;int count1=0,count2=0;for (i=0; i<n-1; i++) //对n个记录进行n-1趟简单选择排序{index=i;for(j=i+1;j<n;j++) //在无序区中选取最小记录{count1++; //比较次数加一if(r[j]<r[index]) //如果该元素比现在第i个位置的元素小index=j;}count1++; //在判断不满足循环条件j<n时,比较了一次if(index!=i){temp=r[i]; //将无序区的最小记录与第i个位置上的记录交换r[i]=r[index];r[index]=temp;count2=count2+3; //移动次数加3 }}for(i=1;i<n;i++)cout<<r[i]<<" ";cout<<endl;cout<<"比较次数为"<<count1<<" 移动次数为"<<count2<<endl;}//筛选法调整堆void Sift(int r[],int k,int m,int &count1,int &count2) //s,t分别为比较和移动次数{int i;int j;int temp;i=k;j=2*i+1; //置i为要筛的结点,j为i的左孩子while(j<=m) //筛选还没有进行到叶子{if(j<m && r[j]<r[j+1]) j++; //比较i的左右孩子,j为较大者count1=count1+2; //该语句之前和之后分别有一次比较if(r[i]>r[j])break; //根结点已经大于左右孩子中的较大者else{temp=r[i];r[i]=r[j];r[j]=temp; //将根结点与结点j交换i=j;j=2*i+1; //下一个被筛结点位于原来结点j的位置count2=count2+3; //移动次数加3 }}}//堆排序void HeapSort(int r[],int n){int count1=0,count2=0; //计数器,计比较和移动次数int i;int temp;for(i=n/2;i>=0;i--) //初始建堆,从最后一个非终端结点至根结点Sift(r,i,n,count1,count2) ;for(i=n-1; i>0; i--) //重复执行移走堆顶及重建堆的操作{temp=r[i]; //将堆顶元素与最后一个元素交换r[i]=r[0];r[0]=temp; //完成一趟排序,输出记录的次序状态Sift(r,0,i-1,count1,count2); //重建堆}for(i=1;i<n;i++)cout<<r[i]<<" ";cout<<endl;cout<<"比较次数为"<<count1<<" 移动次数为"<<count2<<endl;}//一次归并void Merge(int r[], int r1[], int s, int m, int t){int i=s;int j=m+1;int k=s;while (i<=m && j<=t){if (r[i]<=r[j])r1[k++]=r[i++]; //取r[i]和r[j]中较小者放入r1[k]elser1[k++]=r[j++];}if (i<=m)while (i<=m) //若第一个子序列没处理完,则进行收尾处理r1[k++]=r[i++];elsewhile (j<=t) //若第二个子序列没处理完,则进行收尾处理r1[k++]=r[j++];}//一趟归并void MergePass(int r[ ], int r1[ ], int n, int h){int i=0;int k;while (i<=n-2*h) //待归并记录至少有两个长度为h的子序列{Merge(r, r1, i, i+h-1, i+2*h-1);i+=2*h;}if (i<n-h)Merge(r, r1, i, i+h-1, n); //待归并序列中有一个长度小于h else for (k=i; k<=n; k++) //待归并序列中只剩一个子序列r1[k]=r[k];}//归并排序void MergeSort(int r[ ], int r1[ ], int n ){int h=1;int i;while (h<n){MergePass(r, r1, n-1, h); //归并h=2*h;MergePass(r1, r, n-1, h);h=2*h;}for(i=1;i<n;i++)cout<<r[i]<<" ";cout<<endl;}void Newarray(int a[],int b[],int c[]) {cout<<"新随机数组:";c[0]=0;a[0]=0;b[0]=0;for(int s=1;s<11;s++){a[s]=s;b[s]=20-s;c[s]=rand()%50+1;cout<<c[s]<<" ";}cout<<endl;}2.3 其他3. 程序运行结果void main(){srand(time(NULL));const int num=11; //赋值int a[num];int b[num];int c[num];int c1[num];c[0]=0;a[0]=0;b[0]=0;Newarray(a,b,c);cout<<"顺序数组:";for(int j=1;j<num;j++)cout<<a[j]<<" ";cout<<endl;cout<<"逆序数组:";for(j=1;j<num;j++)cout<<b[j]<<" ";cout<<endl;cout<<endl;cout<<"插入排序结果为:"<<"\n";InsertSort(a,num);InsertSort(b,num);InsertSort(c,num);cout<<endl;Newarray(a,b,c);cout<<"希尔排序结果为:"<<"\n";ShellSort(a, num);ShellSort(b, num);ShellSort(c, num);cout<<endl;Newarray(a,b,c);cout<<"起泡排序结果为:"<<"\n";BubbleSort(a, num);BubbleSort(b, num);BubbleSort(c, num);cout<<endl;int count1=0,count2=0;Newarray(a,b,c);cout<<"快速排序结果为:"<<"\n";QuickSort(a,0,num-1,count1,count2);for(int i=1;i<num;i++)cout<<a[i]<<" ";cout<<endl;cout<<"比较次数为"<<count1<<" 移动次数为"<<count2<<endl; count1=0,count2=0;QuickSort(b,0,num-1,count1,count2);for(i=1;i<num;i++)cout<<b[i]<<" ";cout<<endl;cout<<"比较次数为"<<count1<<" 移动次数为"<<count2<<endl; count1=0,count2=0;QuickSort(c,0,num-1,count1,count2);for(i=1;i<num;i++)cout<<c[i]<<" ";cout<<endl;cout<<"比较次数为"<<count1<<" 移动次数为"<<count2<<endl;cout<<endl;cout<<endl;Newarray(a,b,c);cout << "简单选择排序结果为:" << "\n";SelectSort(a,num);SelectSort(b,num);SelectSort(c,num);cout<<endl;Newarray(a,b,c);cout << "堆排序结果为:" << "\n";HeapSort(a, num);HeapSort(b, num);HeapSort(c, num);cout<<endl;Newarray(a,b,c);cout << "归并排序结果为:" << "\n";MergeSort(a, c1,num );MergeSort(b, c1,num );MergeSort(c, c1,num );}。
排序算法的程序实现PPT精品文档

•.
•3
练习1、下表中的原始数据是一组学生的军训打靶成绩, 若采用冒泡排序算法对其进行排序,则第1~4遍的排序 结果分别是()
原始数据 98 95 85 93 88
第1遍
第2遍
第3遍
第4遍
•.
•4
2、陈晓峰和同学们去农科院开展研究性学习,大家都收获很大, 晓峰设计了一个Visual Basic程序,他把同学们收集到的水稻亩 产量和同学姓名已分别保存在数组a和数组b中,第i个同学收集 的亩产量保存在a(i)中,对应的同学姓名保存在b(i)中,最后按 亩产量从高到低进行排序。
Dim b(1 To n) As String
Private Sub Command1_Click()
Dim i As Integer, j As Integer, c As Single, t As String
For i = 1 To n
' 设共有n名同学
For j = n To i + 1 ①
8.0
7.0
9.0
k
8.5
8.0
7.0 8.0
k 8.5
9.0
7.0
7.0
i=3
8.0
8.0
8.5
k 8.5
9.0
9.0
•.
•9
程序:
for i=1 to 3 k=i for j=i+1 to 4 if d(k)>d(j) then k=j next j if k<>i then t=d(i) d(i)=d(k) d(k)=t endif
next i
•.
•10
练习1、下表中的原始数据是一组学生的军训打靶成绩, 若采用选择排序算法对其进行排序,则第1~4遍的排序 结果分别是()
数据结构实验(6)查找和排序

计算机系数据结构实验报告(x)姓名:陈科健学号:6100113017 专业班级:电子商务131 实验名称:查找和排序实验目的:深入了解各种内部排序方法及效率分析。
问题描述:各种内部排序算法的时间复杂度分析,试通过随机数据比较算法的关键字比较次数和关键字移动次数。
实验要求:1、对起泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、堆排序这六种常用排序算法进行分析。
2、用代码实现上述算法中任意两种排序算法。
3、设计待排序表的表长不超过100(其中数据最好用伪随机数产生程序产生,也可以自己设计一组待排序数据)。
4、要对实验结果做简单分析。
算法分析:实验内容和过程:#include<stdio.h>#include<stdlib.h>typedef struct{int key;}Key;typedef struct{Key r[6];}SqList;void BubbleSort(int *a,int n){int i, j,t;for (i = 0; i < n - 1; i++){for (j = 0; j <n-i-1;j++){if (a[j]>a[j+1]){t = a[j];a[j] = a[j+1];a[j+1] = t;}}}for (i = 0; i < n ; i++){printf("%5d", a[i]);}}int Partition(SqList &L, int low, int high){int pivotkey;L.r[0] = L.r[low];pivotkey = L.r[low].key;while (low < high){while (low < high && L.r[high].key >= pivotkey) --high;L.r[low] = L.r[high];while (low < high && L.r[high].key <= pivotkey) ++low;L.r[high] = L.r[low];}L.r[low] = L.r[0];return low;}void QSort(SqList &L, int low, int high){int pivotloc;if (low < high){pivotloc = Partition(L, low, high);QSort(L, low, pivotloc-1);QSort(L, pivotloc + 1, high);}}void QuickSort(SqList &L){int i;QSort(L,0,5);for (i = 0; i < 6; i++){printf("%5d", L.r[i].key);}}int main(){int i, a[6]; SqList L;printf(" ** 起泡排序**\n\n");printf("请输入6个数:\n");for (i = 0; i < 6; i++){scanf_s("%d", &a[i]);}printf("排序后:\n");BubbleSort(a, 6);printf("\n\n ** 快速排序**\n\n");printf("请输入6个数:\n");for (i = 0; i < 6; i++){scanf_s("%d", &L.r[i].key);}printf("排序后:\n");QuickSort(L);system("pause");}实验结果:总结和感想:起泡还好做。
实验七排序

实验七排序
一、目的:
掌握各种排序方法的基本思想、排序过程、算法实现,能进行时间和
空间性能的分析,根据实际问题的特点和要求选择合适的排序方法。
二、
要求:
实现直接排序、冒泡、直接选择、快速、堆、归并排序算法。
比较各
种算法的运行速度。
三、实验内容
1、编写各种排序程序。
2、在排序程序中能输出以上各种排序算法的各趟排序结束时,关键
字序列的状态。
3、调试程序,以关键字序列(265,301,751,129,937,863,742,694,76,438)
作为输入数据,采用上述方法进行排序。
四、实验报告要求
要求所编的程序能正确运行,并提交实验报告。
实验报告的基本要求为:1、陈述程序设计的任务,强调程序要做什么,明确规定:(1)输入
的形式和输出值的范围;(2)输出的形式;
(3)程序所能达到的功能;
(4)测试数据:包括正确的输入输出结果和错误的输入及输出结果。
2、说明用到的数据结构定义、主程序的流程及各程序模块之间的调
用关系。
3、提交带注释的源程序或者用伪代码写出每个操作所涉及的算法。
4、调试分析:
(1)调试过程中所遇到的问题及解决方法;(2)算法的时空分析;(3)经验与体会。
5、用户使用说明:说明如何使用你的程序,详细列出每一步操作步骤。
6、测试结果:列出对于给定的输入所产生的输出结果。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1、直接插入排序2、希尔排序3、2-路归并排序4、折半插入排序5、冒泡排序6、快速排序7、堆排序/*----------------------------------------* 07_排序.cpp -- 排序的相关操作* 对排序的每个基本操作都用单独的函数来实现* 水上飘2011年写----------------------------------------*/// ds07.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"#include "stdio.h"#include <iostream>#include <iomanip>using namespace std;#define MAXSIZE 20typedefintKeyType;typedefstruct{KeyType key; //关键字项KeyType data; //数据项}RedType; //记录类型typedefstruct{RedTypearr[MAXSIZE+1]; //arr[0]闲置或用作哨兵单元int length; //顺序表长度}SqList; //顺序表类型typedefSqListHeapType;//对顺序表L做一趟希尔插入排序//前后记录位置的增量是dk//r[0]只是暂存单元//当j<=0时,插入位置已找到voidshellInsert(SqList&L, intdk){int i, j;for (i = dk + 1; i <= L.length; i++){if (L.arr[i].key <L.arr[i - dk].key){L.arr[0] = L.arr[i]; //暂存for (j = i - dk; j > 0 &&L.arr[0].key <L.arr[j].key; j -= dk){L.arr[j + dk] = L.arr[j]; //记录后移,查找插入位置}//end for jL.arr[j + dk] = L.arr[0]; //插入}//end if}//end for i}//shellInsert//按增量序列dlta[0...t-1]对顺序表做希尔排序voidshellSort(SqList&L, intdlta[], int t){for (int k = 0;k < t; k++){shellInsert(L, dlta[k]); //一趟增量为dlta[k]的插入序列}}//折半插入排序voidbInsertSort(SqList&L){for (int i = 2; i <= L.length; i++){L.arr[0] = L.arr[i];//将其暂存到arr[0]int low = 1;int high = i - 1;while(low <= high){//在arr[low...high]中折半查找有序插入的位置int m = (low + high) / 2;//折半if(L.arr[0].key <L.arr[m].key)high = m - 1;//插入点在低半区else low = m + 1;//插入点在高半区}//whilefor(int j = i - 1; j >= high + 1; j--)L.arr[j + 1] = L.arr[j];//记录后移L.arr[high + 1] = L.arr[0];//插入}//for}//BInsertSort//直接插入排序voidinsertSort(SqList&L){for(int i = 2; i <= L.length; i++){if(L.arr[i].key <L.arr[i-1].key){//if"<",需将L.arr[i]插入有序子表L.arr[0] = L.arr[i];//复制为哨兵L.arr[i] = L.arr[i-1];int j;for(j = i - 2; L.arr[0].key <L.arr[j].key; j--)L.arr[j+1] = L.arr[j];//记录后移L.arr[j+1] = L.arr[0];//插入到正确位置}//if}//for}//InsertSort//冒泡排序voidbubbleSort(SqList&L){RedType* temp = NULL;for(int i = 1; i <L.length; i++){//第i趟排序for(int j = 1; j <= L.length-i; j++){//第j次排序if(L.arr[j].key >L.arr[j+1].key){//交换前后的位置L.arr[0] = L.arr[j];L.arr[j] = L.arr[j+1];L.arr[j+1] = L.arr[0];}//if}//for j}//for i}//bubbleSort//交换顺序表中子表L.arr[low...high]的记录,//枢轴记录到位,并返回其所在位置int partition(SqList&L, int low, int high){L.arr[0] = L.arr[low];//子表的第一个记录做枢轴记录KeyTypepivotKey = L.arr[low].key;//枢轴记录关键字while(low < high){//从表的两端交替向中间扫描while(low < high &&L.arr[high].key >= pivotKey){high--;}//whileL.arr[low] = L.arr[high];//将比枢轴小的记录移到低端while(low < high &&L.arr[low].key <= pivotKey){low++;}//whileL.arr[high] = L.arr[low];//将比枢轴大的记录移到高端}//whileL.arr[low] = L.arr[0];//枢轴记录到位return low;//返回枢轴位置}//paitition//对顺序表L中的子序列L.arr[low...high]做快速排序voidqSort(SqList&L, int low, int high){if(low < high){//长度大于1KeyTypepivotLoc = partition(L, low, high);//将L.arr[low...high]一分为二qSort(L, low, pivotLoc-1);//对低子表递归排序,pivotLoc是枢轴位置qSort(L, pivotLoc+1, high);//对高子表递归排序}//if}//qSort//对顺序表L做快速排序voidquickSort(SqList&L){qSort(L, 1, L.length);}//quickSort//调整H.arr[s]的关键字,使H.arr[s...m]成为一个大顶堆//(对其中记录的关键字而言)voidheapAdjust(HeapType&H, int s, int m){RedTyperc = H.arr[s];for(int j = 2 * s; j <= m; j = j * 2){//沿key较大的孩子结点向下筛选if(j < m &&H.arr[j].key <H.arr[j+1].key){j++;//j为key叫大记录的下标}//ifif(!(rc.key<H.arr[j].key)){break;//rc应插入在位置s上}//ifH.arr[s] = H.arr[j];s = j;}//forH.arr[s] = rc;//插入}//heapAdjust//对顺序表H进行堆排序voidheapSort(HeapType&H){for(int i = H.length/2; i > 0; i--){//把H.arr[1...H.length]建成大顶堆heapAdjust(H, i, H.length);}//forfor(int i = H.length; i > 1; i--){//将堆记录当前未经排序的子序列H.arr[1...i]中的最后一个记录交换H.arr[0] = H.arr[1];H.arr[1] = H.arr[i];H.arr[i] = H.arr[0];heapAdjust(H, 1, i-1);//将H.arr[1...i-1]重新调整为大顶堆}//for}//heapSort//将有序的SR[i..m]和SR[m+1..n]归并为有序的TR[i..n]void Merge(RedType SR[], RedType *TR, int i, int m, int n){int j, k;for (j = m + 1, k = i; i <= m && j <= n; k++)//将SR中记录由小到大地并入TR {if (SR[i].key <= SR[j].key){TR[k] = SR[i++];}else{TR[k] = SR[j++];}}//end for j,kif (i <= m) //将剩余的SR[i..m]复制到TR {for (; k <= n && i <= m; k++, i++){TR[k] = SR[i];}}//end if iif (j <= n) //将剩余的SR[j..n]复制到TR {for (; k <= n && j <= n; k++, j++){TR[k] = SR[j];}}//end if j}//Merge//将SR[s...t]归并排序为TR1[s...t]voidMSort(RedType SR[], RedType TR1[], int s, int t){int m;RedTypeTR2[MAXSIZE];if (s == t){TR1[s] = SR[s];}else{m = (s + t) /2; //将SR[s..t]平分为SR[s..m]和SR[m+1..t]MSort(SR, TR2, s, m); //递归地将SR[s..m]归并为有序的TR2[s..m]MSort(SR, TR2, m+1, t); //递归地将SR[m+1..t]归并为有序的TR2[m+1..t]Merge(TR2, TR1, s, m, t); //将TR2[s..m]和TR2[m+1..t]归并到TR1[s..t] }}//MSort//对顺序表L做归并排序voidmergeSort(SqList&L){MSort(L.arr, L.arr, 1, L.length);}//mergeSort//初始化顺序表,给其赋值voidsetValue(SqList&L){KeyTypedata[8] = {49,38,65,97,76,13,27,49};int n = 8;L.arr[0].data = 0;L.length = n;for(int i = 1; i <= L.length; i++){L.arr[i].data = data[i-1];L.arr[i].key = L.arr[i].data;}//end for i}//setValue//输出顺序表void show(SqList&L){for(int i = 1; i <= L.length; i++){if(i % 5 == 0)cout<<endl;cout<<setw(5) <<L.arr[i].data;}cout<<endl;}//show//调用排序函数进行排序void performance(SqList&L){cout<< "原始序列:" <<endl;show(L);SqList LI = L;insertSort(LI);cout<< "直接插入排序结果:" <<endl;show(LI);SqList LS = L;intdlta[3] = {4,3,1};int t = 3;shellSort(LS, dlta, t);cout<< "希尔排序结果:" <<endl;show(LS);SqList LM = L;mergeSort(LM);cout<< "2-路归并排序结果:" <<endl;show(LM);SqList LB = L;bInsertSort(LB);cout<< "折半插入排序结果:" <<endl;show(LB);SqList LBB = L;bubbleSort(LBB);cout<< "冒泡排序结果:" <<endl;show(LBB);SqList LQ = L;cout<< "快速排序结果:" <<endl;quickSort(LQ);show(LQ);HeapType H = L;cout<< "堆排序结果:" <<endl;heapSort(H);show(H);}//performanceint _tmain(intargc, _TCHAR* argv[]){SqList L;setValue(L);performance(L);system("pause");return 0; }。