数据结构与程序设计(王丽苹)21sorting.ppt
《数据结构与程序设计(王丽苹)21sorting.ppt》由会员分享,可在线阅读,更多相关《数据结构与程序设计(王丽苹)21sorting.ppt(34页珍藏版)》请在三一办公上搜索。
1、5/27/2023,数据结构与程序设计,1,数据结构与程序设计(21),王丽苹,5/27/2023,数据结构与程序设计,2,Chapter 08 排序,插入排序选择排序希尔排序快速排序合并排序堆排序,5/27/2023,数据结构与程序设计,3,Sortable_list新增排序,class Sortable_list:public List public:/Add prototypes for sorting methods here./for quick_sort.void quick_sort();void recursive_quick_sort(int low,int high);in
2、t partition(int low,int high);/for heap_sort.void heap_sort();void build_heap();void insert_heap(const Record,5/27/2023,数据结构与程序设计,4,Mergesort,合并排序思想:We chop the list into two sublists of sizes as nearly equal as possible and then sort them separately.Afterward,we carefully merge the two sorted subli
3、sts into a single sorted list.把待排序的列表划分为分成近似相等的两部分,分别将两个子列表排序,然后再合并成一个完整的列表。,5/27/2023,数据结构与程序设计,5,Mergesort,Mergesort:BOOK P341 FIGURE 8.9Example对26 33 35 29 19 12 22进行合并排序的过程(1)划分为:26 33 35 29 and 19 12 22(2)对子序列1:26 33 35 29 进行合并排序(3)对子序列2:19 12 22进行合并排序(4)合并两个子序列,5/27/2023,数据结构与程序设计,6,Mergesort,
4、void Mergesort(Sortable_list,5/27/2023,数据结构与程序设计,7,Mergesort,void divide_from(Sortable_list/在原序列中删除该节点,5/27/2023,数据结构与程序设计,8,Mergesort,void combine(Sortable_list,5/27/2023,数据结构与程序设计,9,while(mfirstsortlist.size()Record x;firstsortlist.retrieve(m,x);tmp.insert(i+,x);m+;while(nsecondsortlist.size()Reco
5、rd y;secondsortlist.retrieve(n,y);tmp.insert(i+,y);n+;firstsortlist=tmp;,5/27/2023,数据结构与程序设计,10,Mergesort For Linked Lists,class Sortable_list:public List public:/Add prototypes for sorting methods here.void insertion_sort();private:/Add prototypes for auxiliary functions here.;/对sub_list进行合并排序void
6、recursive_merge_sort(Node*,5/27/2023,数据结构与程序设计,11,Mergesort For Linked Lists,void recursive_merge_sort(Node*,5/27/2023,数据结构与程序设计,12,Mergesort For Linked Lists,Node*divide_from(Node*sub_list)/P346/*Post:The list of nodes referenced by sub_list has been reduced to its first half,and a pointer to the f
7、irst node in the second half of the sublist is returned.If thesublist has an odd number of entries,then its first half will be one entry largerthan its second.*/Node*position,/traverses the entire list*midpoint,/moves at half speed of position to midpoint*second_half;if(midpoint=sub_list)=NULL)retur
8、n NULL;/List is empty.position=midpoint-next;while(position!=NULL)/Move position twice for midpoints one move.position=position-next;if(position!=NULL)midpoint=midpoint-next;position=position-next;second_half=midpoint-next;midpoint-next=NULL;return second_half;,5/27/2023,数据结构与程序设计,13,Mergesort For L
9、inked Lists,midpoint,position,midpoint,position,midpoint,position,midpoint,position,5/27/2023,数据结构与程序设计,14,Node*merge(Node*first,Node*second)Node*last_sorted;/points to the last node of sorted listNode combined;/dummy first node,points to merged listlast_sorted=,5/27/2023,数据结构与程序设计,15,Mergesort For
10、Linked Lists,combined,last_sorted,first,last_sorted,head,5/27/2023,数据结构与程序设计,16,Mergesort For Linked Lists,void main()Sortable_list mylist;for(int i=0;i10;i+)mylist.insert(i,Record(10-i,10);coutThe list is:endl;mylist.traverse(print);coutendlUse recursive_merge_sort Method:endl;recursive_merge_sort(
11、mylist.Get_head();mylist.traverse(print);coutendlUse insertion_sort Method:endl;mylist.insertion_sort();mylist.traverse(print);cin.get();,5/27/2023,数据结构与程序设计,17,Mergesort For Linked Lists,template Node*,5/27/2023,数据结构与程序设计,18,Quick_sort,Quicksort思想:We first choose some key from the list for which,we
12、 hope,about half the keys will come before and half after.Call this key the pivot(轴点).Then we partition the items so that all those with keys less than the pivot come in one sublist,and all those with greater keys come in another.Then we sort the two reduced lists separately,put the sublists togethe
13、r,and the whole list will be in order.,5/27/2023,数据结构与程序设计,19,Quicksort:BOOK P343 FIGURE 8.10,19,5/27/2023,数据结构与程序设计,20,Quicksort,66 30 35 29 19 32 35,29 30 35 66 19 32 35,Pivot=29,29 19 35 66 30 32 35,19 29 35 66 30 32 35,35 66 30 32 35 Pivot=30,30 66 35 32 35,66 35 32 35 Pivot=35,35 66 32 35,35 32
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 程序设计 王丽苹 21 sorting

链接地址:https://www.31ppt.com/p-4980199.html