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