{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter 3 - SHEN'S CLASS NOTES-F06 Chapter 3 Comparison...

This preview shows pages 1–6. Sign up to view the full content.

SHEN’S CLASS NOTES-F06 1 Chapter 3 Comparison Sorting A sequence of n numbers, a 1 , a 2 , a 3 , …, a n is said to be in increasing (or, non-decreasing) order if a 1 a 2 a 3 a n . Sorting a given sequence of n numbers is to rearrange them into increasing order. If an algorithm determines the ordering by a sequence of comparisons between two elements (numbers), then this sorting algorithm is called comparison sorting (algorithm). A comparison sorting investigates the relative relations among the numbers rather than knowing the individual values. In this chapter, we will study four major sorting algorithms, namely, the insertion sort, the merge sort, the heap sort, and the quick sort. All of them are comparison sorting. 3.1 Insertion Sort Suppose the n numbers to be sorted are stored in an array A[1. . n ]. The idea of the insertion sort is as follows.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
SHEN’S CLASS NOTES-F06 2 Example. Algorithm Insertion-sort (A[1. . n ]) 1 if n = 1 2 then exit 3 for index 2 to n 4 do { 5 x A[ index ] 6 j index – 1 7 while j > 0 and A[ j ] > x 8 do { A[ j +1] A[ j ] 9 j := j -1 10 } 11 A[ j +1] x 12 } 13 End Complexity Best case, T( n ) = n -1. Worst case, T( n ) = 1 + 2 + 3 + … + ( n -1) = n ( n -1)/2 = Θ ( n 2 ). 5 3 4 1 2 3 5 4 1 2 3 4 5 1 2 1 3 4 5 2 1 2 3 4 5
SHEN’S CLASS NOTES-F06 3 Average case: for index = i , the x = A[ index ] will be at the ( j +1) st position after the sorting. j +1 is in the range of [1, i ]. We assume that Pr[ j +1 = k | 1 k i ] = 1/ i . Then, the average number of comparisons for index = i is i 1 [1 + 2 + … + ( i -1)] + i 1 ( i -1) This is based on the following counting: need i -1 comparisons need 2 comparisons need i -2 comparisons need 1 comparison need i -1 com. also 1 i 2 3 i -1 Therefore, the average number of comparisons for index = i is i 1 i ( i -1)/2 + i 1 ( i -1) = ( i +1)/2 - i 1 . Now the total average number of comparison for the A[1. . n ] is T( n ) = ] 1 2 / ) 1 [( 2 + = n i i i = n ( n +3)/4 -1 - = n i i 2 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
SHEN’S CLASS NOTES-F06 4 = Θ ( n 2 ) – ln n = Θ ( n 2 ). 3.2 Merge Sort The main operation used in the merge sort is called merge . Merge Let A[1. . n ] and B[1. . m ] be two sorted sequences, where A[1] A[2] A[3] A[ n ], B[1] B[2] B[3] B[ m ]. The merge operation will produce array C[1. . n + m ] such that C[1] C[2] C[3] C[ n+m ]. Algorithm Merge (A[1. . n ], B[1. . m ], C[1. . n + m ]) 1 i 1 2 j 1 3 k 1 4 while i n and j m 5 do { if A[ i ] B[ j ] 6 then { C[ k ] A[ i ] 7 i i + 1 8 } 9 else { C[ k ] B[ j ] 10 j j + 1 11 } 12 k k + 1 13 } 14 if i > n 15 then move B[ j .. m ] to C[ k .. n + m ] 16 else move A[ i .. n ] to C[ k .. n + m ]
SHEN’S CLASS NOTES-F06 5 17 End The worst complexity of the merge is W( n , m ) = n + m -1 (comparisons). Note. It can be proved that, for m = n , any merge algorithm needs at least (2 n -1) comparisons in the worst case. We leave the proof for students. Mergesort Based on the merge operation, the merge sort algorithm is as follows. We assume that the numbers to be sorted are stored in the array A, from A[ first ] to A[ last ].

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 29

Chapter 3 - SHEN'S CLASS NOTES-F06 Chapter 3 Comparison...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online