120 likes | 204 Views
Tirgul 5 Notes. This week: Using stacks To eliminate recursion – Towers of Hanoi revisited Converting expressions from infix to postfix Sorting – we can do better than O( n log n )! Bucket sort Counting sort. Using Stacks to Eliminate Recursion. Towers of Hanoi, without recursion…
E N D
Tirgul 5 Notes • This week: • Using stacks • To eliminate recursion – Towers of Hanoi revisited • Converting expressions from infix to postfix • Sorting – we can do better than O(nlogn)! • Bucket sort • Counting sort
Using Stacks to Eliminate Recursion • Towers of Hanoi, without recursion… push( {‘H’, s, t, m, k} ) while ( stack not empty ) x = pop(); if ( x.type == ‘H’ ) if ( x.k>1 ) push( {‘H’,x.s,x.m,x.t,x.k-1} ) push( {‘move’, x.s, x.t} ) push( {‘H’,x.m,x.t,x.s,x.k-1} ) else push( {‘move’,x.s,x.t} ) else // x.type == ‘move’ print( ‘moving x.s->x.’t’);
Infix to Postfix Notation • Expressions as we know them – infix notation • Postfix notation • No ambiguity, no need for parentheses, efficient calculation (see next theoretical exercise..) • Each operator is preceded by its two operands.
Infix to Postfix - Algorithm • read c • if (c is operand) print c • else if (c is ‘(‘) push c • else if (c is ‘)’) • pop all operators from stack until ‘(‘. Do not print parentheses • else if (c is operator) • pop and print all operators of higher priority or left-associative of equal priority • push c onto stack • if end-of-input • pop and print all remaining operators from stack
Infix: Postfix: Infix to Postfix - Example ^ ^ - ^ ^ - ^ - ^ - ( - - - - a - b ^ c ^ c - ( . + ( - . + ( - + ( - + ( - ( - .- . - - . . d + e f ) g
Linear-time Sorting • The general lower-bound running time for sorting algorithms is • The catch • cannot use anything but pair wise comparisons • No prior information on the elements compared • With more information we can do better
Counting Sort • The input – n integers in the range [1,k] • When k = O(n) , the total running time of the sort is also O(n) • The idea • For each element x, how many elements are less than x? If there are 6 elements less than x, it belongs in the 7thposition (in the output) • Running time – O(k)
Counting Sort (2) publicstaticvoid CountingSort(int[] toSort, int[]afterSort, int k ) { int[] c = new int[k]; // now c[i] = 0 for all i for (int i = 0; i<toSort.length ; i++) c[toSort[i]-1] ++; // now c[i-1] holds the number of elements in the input equal to i for (int i = 1 ; i<c.length ; i++ ) c[i] += c[i-1]; // now c[i-1] holds the number of elements in the input which are <= i for (int i=toSort.length-1 ; i>=0 ; i--) { afterSort[ c[ toSort[i]-1 ] -1 ] = toSort[i]; c[toSort[i]-1] --; } }
Bucket Sort • The input – n real numbers in the interval [0,1) (i.e. ), distributed uniformly • The running time is O(n) on the average • The idea • Divide the interval into n ‘buckets’. Put each element into its matching bucket. As the numbers are uniformly distributed, not too many elements will be placed in each bucket – so use insertion sort to sort each bucket, and then concatenate the contents into a single list
Bucket-Sort (2) publicstatic Sequence BucketSort(float[] array) { MySequence[] B = new MySequence[array.length]; for (int i =0 ;i<array.length; i++) B[(int) (n*array[i])].insert(A[i]); // insert each element to the matching bucket. for (int i = 0 ;i<B.length;i++) Utility.InsertionSort(B[i]); // sort each listwith insertion- sort. MySequence result = new MySequence (); for (int i = 0 ;i<B.length;i++) result.add(MySequence); // concatenate the lists together in order. return result; }
Bucket Sort (3) • Running time: • The first loop and last step (concatenation) are O(n). • The insertion sorts: • Denote by nithe number of elements in bucket i. • The expected time to sort bucket i is therefore • The total • An element fits into each bucket with probability 1/n so P(ni=k) is binomial:
Bucket Sort (4) • So we have • The total average running time of the sort is also O(n)