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 …show more content…
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 …show more content…
Assume characters are compared based on alphabetical order. Explain your answer. [5 marks] [TOTAL: 20 MARKS] 5
CMDA5103/MAY2009-F/FA
Question 6 A hash table is a data structure that uses an array as its internal storage container. Items are added to the array based on the integer generated by a hash function. A hash function produces an integer based on some properties of the object. In Java hash functions are encapsulated via the hashcode method in the Object class and that many classes override.
To add an Object to a hash table:
1. call its hashcode method to obtain an int, x 2. modulus x by the length of the hash table to obtain an index, i 3. go to index i in the hash tables native array 4. if that element is null, place the Object in that element 5. if that element is not null a collision has occurred. Collisions can be resolved in many ways. 6. In this hash table collisions are resolved via linear probing. Check the element at index i +1 7. If the element at i + 1 is null, add the item at that element, if not continue to check elements until an open spot is found, by adding 1 to the index to be checked. If the end of the array is reached then