{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS300-06_Sorting

# CS300-06_Sorting - Sorting Sung Yong Shin TC Lab CS Dept...

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

11 Sorting Sung Yong Shin TC Lab. CS Dept., KAIST

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

View Full Document
22 1. Introduction 2. Insertion Sort 3. Quick Sort 4. Merge Sort 5. Heap Sort 6. Shell Sort 7. Radix Sort Reading Assignment p149-222, Baase et al. Outline
33 25 - 50 % of Computing time !!! Reporting Updating Sorting and Searching Inquiring Rich Results, e.g., Volume 3, Art of Programming (D. Knuth) Internal Sorting The file to be sorted is small enough so that the entire sort can be car- ried out in main memory. Time complexity better External Sorting 1. Introduction

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

View Full Document
44 SORT : Given a set of n real numbers , rearrange them in the increasing order. Given a file of n records (R1, R2, R3, …, Rn ) with keys ( k 1, k 2, …, kn ), find a permutation such that 1 2 3 n Record 1 key 2 3 n note : keys are not nec. real num- bers } , , 3 , 2 , 1 { } , , 3 , 2 , 1 { : n n Π j i k k j i Π < Π ) ( ) (
55 Lower Bound (Worst case) Stability A sorting method is stable if equal keys remain in the same relative order in the sorted list as they were in the original list. In place An algorithm is said to be in place if the amount of extra space is constant with respect to input size. Time complexity Worst case Average case …………… …… Decision Tree 1 (n!) leaves T SORT = ( log2 n !) = ( n lo- g2 n )

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

View Full Document
66 2. Insertion Sort When the ith element is examined, the first i-1 elements have already been sorted. Thus, the ith element is inserted into the proper place in the sorted sublist. Where to insert?
77 Algorithm (Insertion Sort) procedure InserionSort (var L : array ; n : integer ) ; var x : Key ; xindex , j : Index ; begin for xindex = 2 to n do x := L ( xindex ) j := xindex - 1 ; while j > 0 and L ( j ) > x do L ( j+ 1) := L ( j ) ; j := j -1 ; end {while} L ( j+ 1) := x; end {for}

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

View Full Document
88 T SORT( n ) = ( n log n ) T ( n ) ? ( Worst case ) xindex # of comparisons 2 1 3 2 4 3 i i -1 n n -1 1 Total # of comparisons = T ( n ) = O( n 2) Far from Optimal !!! However, …… 2 ) 1 ( n- n
99 Assumption : The i th element 1≤ i n is equally likely to be placed at each of i positions. Keys are distinct. Observation: P( The i th element is in the j th position ) = 1/ i , j = 1, 2, …, i Ai ( n ) = the average # of comparisons for the i th element A ( n ) = the sum of average numbers of comparisons for all i A ( n ) = i possible posi- tions = n i i n A 1 ) ( Average Behavior ……

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

View Full Document
0 if i = 1 Ai ( n ) = A ( n ) = 1010 - = - + = - + × 1 1 1 2 1 ) 1 ( 1 1 i j i i i i j i = = - - + = - + n i n i i n n i i 2 2 2 1 1 4 3 4 ) 1 2 1 ( 4 2 n 2245 n ln 2245 (See page 26) 1 O( n 2 ) , otherwise
1111 Assumption : (1) Compare adjacent keys (2) Depending on the result, move the current pair of compared keys locally. What kind of sorting algorithms under these two assumptions?

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 / 40

CS300-06_Sorting - Sorting Sung Yong Shin TC Lab CS Dept...

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

View Full Document
Ask a homework question - tutors are online