Chapter 3 - SHENS CLASS NOTES-F06 Chapter 3 Comparison...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: SHENS CLASS NOTES-F06 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. 1 SHENS CLASS NOTES-F06 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 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 2 SHENS CLASS NOTES-F06 Best case, T( n ) = n-1. Worst case, T( n ) = 1 + 2 + 3 + + ( n-1) = n ( n-1)/2 = ( n 2 ). 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 . 3 SHENS CLASS NOTES-F06 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 = ( 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 4 SHENS CLASS NOTES-F06 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 ] 17 End The worst complexity of the merge is W( n , m ) =...
View Full Document

This note was uploaded on 04/12/2008 for the course CS 592 taught by Professor Shen during the Fall '05 term at University of Missouri-Kansas City .

Page1 / 30

Chapter 3 - SHENS CLASS NOTES-F06 Chapter 3 Comparison...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online