天津大学:《数据结构 Data Structures》课程PPT教学课件(英文版)Chapter 8 Sorting

CHAPTER 6 SORTING s 1 Preliminaries Sorting: There is a series of data in random order, we sort them depending a certain key word Datalist: an finity set of data waiting to be sorted Key: data object has many attribute areas, namely there are many data element, one of all these elements can be used to distinguish object, we use it as sorting key. we also call it sorting code
◼ Sorting: There is a series of data in random order, we sort them depending a certain key word. ◼ Datalist: an finity set of data waiting to be sorted. ◼ Key: data object has many attribute areas, namely there are many data element, one of all these elements can be used to distinguish object, we use it as sorting key. we also call it sorting code. CHAPTER 6 SORTING §1 Preliminaries

The stability of sorting method: if there are two objects r and rlil in the object series, and their sorting codes are k==klil,And before sorting, object rli is in front of object r[ji. If after the sorting, objectrlil is still in front of object rIil, then we call this sorting method stable, otherwise we call this sorting method unstable. Inner sort and outer sort: inner sort means in the sorting period, all the data objects are in the memory. Outer sort means in the sorting period, since the number of object is too large, and they cannot be put in the memory at the same time. We must move the sorting between inner and outer memory according to the demand
◼ The stability of sorting method: if there are two objects r[i] and r[j] in the object series, and their sorting codes are k[i] == k[j],And before sorting, object r[i] is in front of object r[j] .If after the sorting, object r[i] is still in front of object r[j] , then we call this sorting method stable, otherwise we call this sorting method unstable. ◼ Inner sort and outer sort: inner sort means in the sorting period, all the data objects are in the memory. Outer sort means in the sorting period, since the number of object is too large, and they cannot be put in the memory at the same time. We must move the sorting between inner and outer memory according to the demand

Time spending of sort: time spending of sort is an important standard to measure whether a method is good or not. Time spending of sort can be measured using data compare times and data moving times
◼ Time spending of sort: time spending of sort is an important standard to measure whether a method is good or not. Time spending of sort can be measured using data compare times and data moving times

TThe kinds of inner sort according to different principle insertion sort、 exchange sort、 selection sort、 merge sort.、 counting sort according to workload simple sort---time complexity o(n2) advanced sort--- time complexity o(n logn) base sort-- time complexity o(d n)
The kinds of inner sort • according to different principle insertion sort、exchange sort、selection sort、merge sort、counting sort. • according to workload simple sort---time complexity o(n2) advanced sort--- time complexity o(n logn) base sort--- time complexity o(d.n)

82 Insertion Sort )Direct Insertion sort 5 temp 21)(25)(49 35 6)(08 i=1 234 5 temp 21 21)(25)(49 5 08 i=2②5(@ 22(96 3②2②59 0 229 08
➢Direct Insertion sort i = 1 0 1 2 3 4 5 temp i = 2 0 1 2 3 4 5 temp 21 25 49 25* 16 08 21 25 49 25* 16 08 25 21 25 49 25* 16 08 21 25 49 25* 16 08 49 21 25 49 25* 16 08 i = 3 21 25 49 25* 16 08 25* 21 25 25* 49 16 08 §2 Insertion Sort

22)49O的8 162因25(9@9 16 25)25(49 i=5 08(四21(252549
i = 4 i = 5 21 25 25* 49 16 08 16 16 21 25 25* 49 08 16 21 25 25* 49 08 08 16 21 25 25* 49 08

Direct Insertion Sort Algorithm void Insertion Sort( ElementType A[], int N) ElementType Tmp; for(P=1;P≤N;P++){ Tmp=A[P]: the next coming card*/ for(j=P; j>0&&A[j-1]> Tmp; j-) A[j]=A[j-1]; shift sorted cards to provide a position for the new coming card"/ A[j]= Tmp;/ place the new card at the proper position*/ 3 /end for-P-loop * The worst case: Input al is in reverse order. T(N)=O(N2) The best case: Input al is in sorted order. T(N)=O(N)
Direct Insertion Sort Algorithm void InsertionSort ( ElementType A[ ], int N ) { int j, P; ElementType Tmp; for ( P = 1; P 0 && A[ j - 1 ] > Tmp; j-- ) A[ j ] = A[ j - 1 ]; /* shift sorted cards to provide a position for the new coming card */ A[ j ] = Tmp; /* place the new card at the proper position */ } /* end for-P-loop */ } The worst case: Input A[ ] is in reverse order. T( N ) = O( N2 ) The best case: Input A[ ] is in sorted order. T( N ) = O( N )

)Binary Insertion sort Basic idea: there are number of n sequential objects vIOL vIIL,.,VIn-l in the seqlist, Insertion sort makes use of the fact that elements in position 0 through i-I are already in sorted order. use binary search to find the inserting position of vi when insert element vi Algorithm of binary insertion sort
➢Binary Insertion sort Basic idea: there are number of n sequential objects V[0], V[1], …, V[n-1] in the SeqList,Insertion sort makes use of the fact that elements in position 0 through i-1 are already in sorted order. use binary search to find the inserting position of V[i] when insert element v[i]. Algorithm of binary insertion sort

typedef int SortData; void BinInssort( SortData VI, int n)i SortData temp; int Left, Right; for(int i=l; i= Left; k--)VIk+1=VIk 记录后移 VLLeft=temp;插入
typedef int SortData; void BinInsSort ( SortData V[ ], int n ) { SortData temp; int Left, Right; for ( int i = 1; i = Left; k-- ) V[k+1] = V[k];// 记录后移 V[Left] = temp; //插入 } }

Binary insertion sort 234 5 temp 2(5 5 0 2 4 5 temp i=22( 3 i=4(34⑤2(8(8 i=5(3((8②0的 ③3④46(8662
Binary insertion sort 0 1 2 3 4 5 temp i = 1 i = 2 0 1 2 3 4 5 temp 5 21 3 3 i = 3 5 5 3 5 21 21 4 4 i = 4 3 5 21 8 8 i = 5 3 5 8 21 16 16 4 4 3 4 5 8 16 21
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 天津大学:《数据结构 Data Structures》课程PPT教学课件(英文版)Chapter 7 Search.ppt
- 天津大学:《数据结构 Data Structures》课程PPT教学课件(英文版)Chapter 6 Graph Algorithms.ppt
- 天津大学:《数据结构 Data Structures》课程PPT教学课件(英文版)Chapter 5 trees.ppt
- 天津大学:《数据结构 Data Structures》课程PPT教学课件(英文版)Chapter 4 Stacks Queues.ppt
- 天津大学:《数据结构 Data Structures》课程PPT教学课件(英文版)Chapter 3 Lists.ppt
- 天津大学:《数据结构 Data Structures》课程PPT教学课件(英文版)Chapter 2 Algorithm Analysis.ppt
- 天津大学:《数据结构 Data Structures》课程PPT教学课件(英文版)Chapter 10 The Disjoint Set ADT.ppt
- 成都理工大学工程技术学院:《C程序设计教程》第九章 变量的作用域与生存期.ppt
- 成都理工大学工程技术学院:《C程序设计教程》第八章 文件访问.ppt
- 成都理工大学工程技术学院:《C程序设计教程》第七章 结构体与共用体.ppt
- 成都理工大学工程技术学院:《C程序设计教程》第六章 函数.ppt
- 成都理工大学工程技术学院:《C程序设计教程》第五章 指针.ppt
- 成都理工大学工程技术学院:《C程序设计教程》第四章 数组.ppt
- 成都理工大学工程技术学院:《C程序设计教程》第三章 程序的控制结构.ppt
- 成都理工大学工程技术学院:《C程序设计教程》第二章 C程序设计基础.ppt
- 成都理工大学工程技术学院:《C程序设计教程》第一章 C程序概述.ppt
- 《vc++课件》类的设计和对象的使用.ppt
- 《vc++课件》c++基础2.ppt
- 《vc++课件》c++基础1.ppt
- 《vc++课件》对话式应用程序设计.ppt
- 天津大学:《数据结构 Data Structures》课程PPT教学课件(英文版)Chapter 9 String.ppt
- 《C++程序设计》第十一讲 输出与输入.ppt
- 《C++程序设计》第二讲 C++语言基础.ppt
- 《C++程序设计》第九讲 派生与继承性.ppt
- 《C++程序设计》第六讲 类与对象.ppt
- 《C++程序设计》第七讲 类与对象.ppt
- 《C++程序设计》第三讲 C++语言基础.ppt
- 《C++程序设计》第十二讲 输出与输入.ppt
- 《C++程序设计》第十讲 虚函数与多态性.ppt
- 《C++程序设计》第八讲 类与对象.ppt
- 《C++程序设计》第四讲 C++语言基础.ppt
- 《C++程序设计》第五讲 类与对象.ppt
- 《C++程序设计》第一讲 面向对象程序设计.ppt
- 《10步之内学会 Photoshop CS》(英文版)Adobe® Photoshop® CS in 10 Simple Steps or Less.pdf
- 复旦大学:《数据通讯与计算机网络》课程教学资源(PPT课件)第一章 概论(高传善).ppt
- 复旦大学:《数据通讯与计算机网络》课程教学资源(PPT课件)第十章 网络管理基础和网络安全性 10.1 网络管理基础 10.2 数据加密.ppt
- 复旦大学:《数据通讯与计算机网络》课程教学资源(PPT课件)第十章 网络管理基础和网络安全性 10.3 网络安全.ppt
- 复旦大学:《数据通讯与计算机网络》课程教学资源(PPT课件)第二章 数据通信基础.ppt
- 复旦大学:《数据通讯与计算机网络》课程教学资源(PPT课件)第三章 数据链路层 3.1 差错检测与校正 3.2 数据链路层的功能.ppt
- 复旦大学:《数据通讯与计算机网络》课程教学资源(PPT课件)第三章 数据链路层 3.3 数据链路协议 3.4 协议描述与验证 3.5 链路通信规程举例.ppt