CS5302 Question 1. Midterm Analyze the running time complexity (in terms of big O notation) of the following algorithms. Algorithm 1: for (i=0; i<a.length-1; i++) { for (j=i+1; j<a.length; j++) { if(a[i] > a[j]) { /* switch a[i] and a[j] */ tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } Algorithm 2: public class recu{ public static int recursiveFactorial(int n){ if (n == 0) return 1; else return n*recursiveFactorial(n-1); } public static void main(String[] args ) { int t ; t=recursiveFactorial(6); System.out.println("P"+t); } } Algorithm 3: import java.util.Random; import java.util.Date; import net.datastructures.HeapPriorityQueue; import net.datastructures.Entry; public class HeapSort { final static int DEFAULT_ELEMENT_NUM=30000; public static void main (String[] args) { int i, j, tmp; int[] a; int n; /* the number of elements in the array */ if (args.length == 0) n = DEFAULT_ELEMENT_NUM; else n = Integer.parseInt(args[0]); a = new int[n]; /* set the initial value randomly for the ARRAY */ Random r = new Random(); for (i=0; i< a.length; i++) { a[i] = r.nextInt(n); } /* create an empty heap */ MyComparator c = new MyComparator(); HeapPriorityQueue h = new HeapPriorityQueue(c); /* get the starting time */ Date start = new Date(); /* heap sort */ for (i=0;i<a.length;i++) { h.insert(a[i],a[i]); } i = 0; while ( !(h.isEmpty()) ) { Entry e = h.removeMin(); a[i] = ((Integer)e.key()).intValue(); i++; } } } Assume that each heap operation takes O(log n) time. Question 2. The Parentheses Matching Algorithm is used to test if the parentheses in an expression match. Parentheses Matching Algorithm Algorithm ParenMatch(X,n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let S be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then S.push(X[i]) else if X[i] is a closing grouping symbol then if S.isEmpty() then return false {nothing to match with} if S.pop() does not match the type of X[i] then return false {wrong type} if S.isEmpty() then return true {every symbol matched} else return false {some symbols were never matched} Consider the expression [(1+2 )+ ( (2+2 )* ( 3+5) )]. Show the contents of the stack while executing the algorithm. Question 3. The following program creates a linked list with head1 as its head. Add the codes at the end of main() so that the first node in the list is deleted. public class list1 { public static void main(String[] args ) { int i; Node head1, tail1, head2, tail2, temp; Node head3, tail3; /***** Creating list 1 Node u = new Node(0, null); head1=u; tail1=u; for (i=1; i<=10; i++) { temp = new Node(i, null); tail1.setNext(temp); tail1=temp; } /*** add the code here } } ******/ ********/ Question 4. Consider the following binary tree. A B D C E H F G I Give the Inorder, Postorder and Preorder traversals of the binary tree. Question 5. Use the Array-Based Representation of Binary Trees to store the binary tree given in Question 4. Let a be the array that stores the tree. parent of a[i]? where is the left Question 6. Suppose that a node is stored in a[i]. Where is the and right children of a[i]? Let a={21, 29, 33, 1, 6, 3, 28, 11, 9, 13, 8, 39, 99, 19, 95, 98, 88, 58, 66} be a set of numbers. Use the linear time algorithm to build a heap containing the set of numbers in a. Question 7. Suppose a heap is as follows: 11 12 13 14 19 15 20 21 24 16 25 26 17 27 28 (a) Show the procedure to insert a node with key 2. (b) For the same heap (without the node with key 2), show the procedure to removeMin. Give the keys 39, 0, 1, 28, 24, 20, 11 and 13, insert keys (according to the given order) into an initially empty 13-item hash table using hash function h(k ) k mod 13 where k represents a key. Show the content of Question 8. hash table after insertion, assuming collisions are handled by i. ii. Linear probing Double hashing h ' (k ) 1 (k mod 11) . where the secondary hash function is