INSTRUCTIONS: 1. THERE ARE SIX (6) QUESTIONS IN THIS PAPER. 2. ANSWER FIVE (5) QUESTIONS ONLY. Question 1 Arrays are used when storing a large number of values. You are required to create an array named a and answer the following questions regarding array manipulation. a. Write a method fillRandom(int[] a, int min, int max), fill the array a with a random integer value. (Note: Math.random() returns a double in the range of 0.0 and 1.0, therefore it is cast to an integer number, between the minimum and maximum value). [6 marks] b. Write the Bubble sort method to sort array a into descending order. [10 marks] c. In the quicksort, an algorithm an element is chosen from the unsorted list. This element is called the pivot. The unsorted list is then partitioned into elements less than or equal to the pivot and elements greater than the pivot. These two sub-lists are then quicksorted. In the average case quicksort is O(N log N). The worst case quicksort is O(N2). Explain what must occur for this worst case behaviour to occur. [4 marks] [TOTAL: 20 MARKS]

1

CMDA5103/MAY2009-F/FA

Question 2 A stack is a data structure that allows data to be inserted (a 'push' operation), and removed (a 'pop' operation). Many stacks also support a read ahead (a 'peek' operation), which reads data without removing it. A stack is a LIFO-queue, meaning that the last data to be inserted will be the first data to be removed. a. Given data: 34, 20, 15, 48, and 8. Draw the diagram of a stack named S after all the values are entered. [5 marks] b. Write a java program that will create a new stack S1, insert 10 integer values into it. Then create another stack S2, which will contain the reverse of stack S1 integer value. Your program will include methods push (), pop (), peek () and empty (). Display the contents S1 and S2 stacks. [15 marks] [TOTAL: 20 MARKS] Question 3 a. Write a recursive method Multiply that will multiply 2 numbers, x and y by adding these numbers repeatedly. Example: to multiply 2 and 3, you will add 2, 3 times. (2 * 3 == 2 + 2 + 2. [10 marks] b. A Fibonacci sequence is as follows: 1 1 2 3 5 8 13 21 34 55 … Each number is the sum of the preceding two numbers in the sequence. Write the Fibonacci recursive method that will calculate the nth Fibonacci number. [10 marks]

[TOTAL: 20 MARKS] 2

CMDA5103/MAY2009-F/FA

Question 4 a. Describe the following, using illustration: (i) singly linked list, (ii) circular linked list, and (iii) doubly linked list. [10 marks] b. Given the class Link below. Write methods called, i) insertFirst, that will insert a new node at the beginning of the link list. [3 marks] ii) deleteEnd, that will delete the node at the end of the list. [3 marks] iii) numberOfLink, that will count the number of data links created for the class Link. [4 marks] class Link { public int iData; // data item public double dData; // data item public Link next; // next link in list // ------------------------------------------------------------public Link(int id, double dd) // constructor { iData = id; // initialize data dData = dd; // ('next' is automatically } // set to null) // ------------------------------------------------------------public void displayLink() // display ourself { System.out.print("{" + iData + ", " + dData + "} "); } } // end class Link //////////////////////////////////////////////////////////////// class LinkList { private Link first; // ref to first link on list // ------------------------------------------------------------public LinkList() // constructor { first = null; // no links on list yet } // ------------------------------------------------------------public boolean isEmpty() // true if list is empty { return (first==null);

3

CMDA5103/MAY2009-F/FA

} // ------------------------------------------------------------// insert at start of list

// ------------------------------------------------------------// delete last item

//...