Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Ari’s class: PHO 211 – 2/7/06 1. heapsort (I've already introduced min- and max- heaps) 2. information-theoretic lower-bound on comparison-based sorting 3. counting sort (aka bin sort) 4. radix sort 5. bucket sort 1 Priority Heaps Basic Heap ADT: Data is a[i] i = 1,...,N Methods: Insert (key), Delete(key), DeleteMin, Build and Sort Q: When is a tree an array? A: complete tree Parent a[i] a[2i] = left child & a[2i+1] = right child Child a[j]: a[ j/2 ] = parent (integer division). Build Heap is O(N) and DeleteMin is O(log(N)) Heap sort by deleting min over and over is O(Nlog(N)). 2 Min Heap Order depth =0/ height = 3 1 depth =1/height = 2 depth =2/height=1 24 height =4 9 2 23 10 25 Partial Ordering 11 3 18 4 120 depth = 3/heigth = 0 3 Algebraic Details for TH(N) vs TD(N) 4 Week 4: Lecture 7, Sept 23, 2002 Proof of (Nlog(N)) Decision Tree a, b, c a<b a<b, c b<a, c a<c b<c a< b ,a<c c< a < b b <a,c<a c < b <a c< a b<c a<b< c a< c < b b< a < c b<c< a Binary decisions: 3! = 6 possible outcomes. Longest path: log(3!) 5 Lower Bound Theorem for Camparision Sort Proof: Compute the maximum depth D of decision tree? Need N! leaves to get all possible outcomes of a sorting routine. Each level at most doubles: 1 2 4 8 2D Consequently for D levels: T ( N ) ( D) (log 2 ( N!)) ( N log 2 ( N )) Information log 2 ( N!) N log 2 ( N ) Number of bits to encode any (initial) state is information ( - Entropy) 6 Bisection Search of Sorted List int a[0], a[1],a[2],a[3],.... a[m],.... a[2],a[N-1] i j i= 0; j= N-1; m = N/2 while(b!=a[m] && i!=j ){ if(b>a[m]) i = m; if(b<a[m]) j = m; m = (j-i)/2 + i;} if(b==a[m])) “found it” else “not found” T(N) = T(N/2) + c0 Choose mid point T(N) » Log(N) 7 Dictionary: Sorted and Uniform int a[0], a[1],a[2],a[3],.... a[m],.... i Dictionary: Same code EXCEPT estimate location of b x = fractional distance (0<x<1) x = (b-a[i])/(a[j] – a[i]) ; m = x (j-i) + i ; T(N) = T(N1/2) + c0 a[2],a[N-1] j m T(N) » Log(Log(N)) Extra Knowledge Helps: % Error » 1/N1/2 8 O(N): Bin, Bucket & Radix BIN Sort – make histogram: N integers 0 < a[i] < M in the range v= 0,...,M-1. Count number of occurrences in a[i] for(v=0; v<M; v++) bin[ v ] =0; for(i=0;i<N; i++) bin[a[i]] ++; j=0; for(v=0; v< M; v++) { for(i=0; i<bin[v]; i++) a[ j ] = v; j++; } O(M + N) so if M » N it is O(N) 9 Bucket Sort Choose B Buckets as bins for high digits of a[i] place N numbers in a[i] in buckets Sort average of N/B elements in each bucket. Linked list: Bucket: 0 1 2 3 4 5 6 O( N + B*(N/B log(N/B) ) = O( N + N log(N/B)) 7 8 B = O(N) 9 10 Radix Sort (IBM Card Sorter!) Represent integers in a[i] in base B: n0 + n1 B + n2 B2 + .... + np BP Sort into buckets by low digits first: n0, then n1, etc. Queues: B= 10 Example: 64, 8, 216, Bucket: 0 1 2 #1 0 1 512 #2 8 1 0 216 512 729 27 125 #3 64 27 8 1 0 125 216 3 343 4 64 512, 0, 1, 343, 125 5 6 7 8 9 125 216 27 8 729 343 343 27, 729, 64 512 729 O( N P ) where BP = M or P= log(M) / log(B) = O(1) 11 End Ari’s Class 12