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

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

1 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
2 1. Introduction 2. Insertion Sort 3. Quick Sort 4. Merge Sort 5. Heap Sort 6. Shell Sort 7. Radix Sort Internal 8. External Sorting External Reading Assignment p149-222, Baase Contents
3 25 - 50 % of Computing time !!! Reporting Updating Sorting, Searching Queries Rich Results Internal Sorting The file to be sorted is small enough so that the entire sort can be carried out in main memory. ( Time complexity better ) External Sorting ( # of I/O operations better ) 1. Introduction

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

View Full Document
4 SORT : Given a set of n real numbers , rearrange them in the increasing order. Given a file of n records (R 1 , R 2 , R 3 , …, R n ) with keys ( k 1 , k 2 , …, k n ) find a permutation such that 1 2 3 n Record 1 key 2 3 n …… note : keys are not nec. real numbers } , , 3 , 2 , 1 { } , , 3 , 2 , 1 { : n n Π j i k k j i < < ) ( ) ( Π Π
5 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 Average …………… …… Decision Tree (n!) leaves T SORT = ( log 2 n !) = ( n log 2 n )

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

View Full Document
6 2. Insertion Sort
7 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} end Correctness Proof Exercise. Hint : Loop invariant ( induction on xindex )

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

View Full Document
8 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 Total # of comparisons = T ( n ) = O( n 2 ) Far from Optimal !!! However, …… 2 ) 1 ( n- n
9 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 is in the j th position ) = 1/ i , j = 1, 2, …, i A i ( 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 positions = n i i n A 1 ) ( Average Behavior ……

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

View Full Document
10 - = - + = - + × 1 1 1 2 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 0 if i = 1 A i ( n ) = A ( n ) = (See page 26) O( n 2 )
11 Assumption : (1) Compare adjacent keys (2) Depending on the result, move the curent pair of compared keys locally.

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.

This note was uploaded on 02/04/2010 for the course COMPUTER S cs300 taught by Professor Unkown during the Spring '08 term at Korea Advanced Institute of Science and Technology.

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