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
1 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
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
Background image of page 2
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
Background image of page 3

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

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

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

View Full DocumentRight Arrow Icon
6 2. Insertion Sort
Background image of page 6
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 )
Background image of page 7

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

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

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

View Full DocumentRight Arrow Icon
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 )
Background image of page 10
11 Assumption : (1) Compare adjacent keys (2) Depending on the result, move the curent pair of compared keys locally.
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.

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