Prog Tutorial5 PDF
Prog Tutorial5 PDF
1. Write a program to find the number of comparisons using binary search and sequential search
algorithms as follows:
Suppose the list contains 100 elements:
a. Use a random number generator to fill the list for number from 1 to 200 without
duplication.
b. Use any sorting algorithm to sort the list.
c. Print out the number of comparisons required to find (or not find) every element from 1 to
200.
d. Calculate the average number of searches for each algorithm.
e. Repeat (a) to (d) 20 times to obtain the average of the average.
2. Write a program to implement a type of searching algorithm called random searching. Consider
an array of size N, at each search, a random location in the array is chosen and compared with the
‘key’, if it is found, the program will report that the element is found. Otherwise, another random
location is then chosen to compare. This is done until the element is found, or the stopping criteria
is fulfilled. What is the stopping criteria? Also, it is important to ensure that each random location
chosen has not been chosen previously. Write a main to demonstrate the code.
Compare the efficiency of this search algorithm with sequential and binary search.
3. Write a program to test mergesort for a linked list of object class Person, which contains data
member string name, and char gender. The sorting is first according to gender (F then
M) then name (A to Z).
4. Write a program to demonstrate quicksort for a linked list object class Student, which contains
data member string name and string metric. Allow the user to choose to sort according
to name or metric number (metric).
5. Write a program to implement the following sorting algorithm for integers:
sortX(arr1[], n)
1) Create n empty arrays, arr2[].
2) For every element in arr1, insert arr1[i] into
arr2[n*arr1[i]]
3) Sort every arr2 using insertion sort.
4) Concatenate all sorted arr2.
---END---