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

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

View Full Document Right Arrow Icon
11 Sorting Sung Yong Shin TC Lab. CS Dept., KAIST
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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 Π < Π ) ( ) (
Background image of page 4
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 )
Background image of page 5

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

View Full DocumentRight Arrow Icon
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?
Background image of page 6
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}
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
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 ……
Background image of page 9

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

View Full DocumentRight Arrow Icon
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
Background image of page 10
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?
Background image of page 11

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

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

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 Right Arrow Icon
Ask a homework question - tutors are online