DS-chapter6(sorting-2)

DS-chapter6(sorting-2) - 6.9 A General Lower Bound for...

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

View Full Document Right Arrow Icon
6.9 A General Lower Bound for Sorting . Theorem h Any algorithm that sorts by comparisons only must have a worst case computing time of ( N log N ). Proof: K 0 K 1 K 1 K 2 K 0 K 2 stop [0,1,2] stop [0,2,1] stop [2,0,1] T F T F K 0 K 2 K 1 K 2 stop [1,0,2] stop [1,2,0] stop [2,1,0] T F T F T F Decision tree for insertion sort on R 0 , R 1 , and R 2 When sorting N distinct elements, there are N ! different possible results . Thus any decision tree must have at least N ! leaves . If the height of the tree is k , then N ! 2 k (# of leaves in a complete binary tree) k log( N !) Since N ! ( N /2) N /2 and log 2 N ! ( N /2)log 2 ( N /2) = Θ ( N log 2 N ) Therefore T ( N ) = k c N log 2 N . LEMMA 6.1 Let T be a binary of depth d . then T has at most 2 d leaves LEMMA 6.2 A binary tree with L leaves must have depth at least log L
Background image of page 1

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

View Full DocumentRight Arrow Icon
Sorting Large Structures Problem: Swapping large structures can be very much expensive. Solution: Add a pointer field to the structure and swap pointers instead indirect sorting . Physically rearrange the structures at last if it is really necessary. list key table [0] d 0 [1] b 1 [2] f 2 [3] c 3 [4] a 4 [5] e 5 table 4 1 3 0 5 2 The sorted list is list [ table[0] ], list [ table[1] ], ……, list [ table[n - 1] ] Note: Every permutation is made up of disjoint cycles. . Example . Table Sort The cycle for any element i consist of i , T 1 [ i ],…, T k [ i ], where T j [ i ] = T [ T j-1 [ i ]], T 0 [ i ] = i , and T k [ i ] = i . Cycle1:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 13

DS-chapter6(sorting-2) - 6.9 A General Lower Bound for...

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

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