doc

advertisement
CSE1301 Semester 1, 2001
Exercise Sheet 9
CSE1301 Exercise Sheet 9 – Week May 14-18, 2001
Lists: Searching and Manipulation
Exercise 1
(a) Set up a list of numbers and construct a set of test data to test the binary search algorithm.
Work through each example, making sure you understand how binary search works.
(b) Modify the binary search function so that it returns the position of a sought number (rather
than true) if the item is found, and it returns –1 if the item is not found.
(c) Modify the binary search function so that it returns the position of a sought name (character
string).
(d) Modify the binary search function so that it finds a student by his/her name or ID number in
an array of structs such as that defined in a struct of type ClassType (see Lecture 26).
(e) What is the best case performance of binary search? What is the worst case performance?
Exercise 2
Consider the operations Find, Add and Delete. How are these operations performed on a sorted
list and an unsorted list? Compare the time complexity of these operations when applied to a
sorted list and when applied to an unsorted list.
Exercise 3
An array of size N is being used to store a list of names (strings) sorted in alphabetical order.
a) Modify the algorithm addElement and the corresponding function presented in class so that it
handles names, and it does not allow the insertion of a name that already appears in the list.
b) Write an algorithm to delete a name from the list. Code your algorithm as a C function called
deleteElement.
c) Modify the functions addElement and deleteElement so that they can add an element to or
delete an element from an array of structs.
Exercise 4
Suppose you have two arrays of names that are used to store 2 separate lists which are sorted in
ascending order. Write an algorithm to merge these two lists into a new list (in a new array),
which is also sorted in ascending order. Code your algorithm in C.
T9: Lists (Search and Manipulation)
Page 1 of 2
CSE1301 Semester 1, 2001
Exercise Sheet 9
Exercise 5 (Complexity)
The prime factors of a given number are a unique series of prime numbers which multiply together
to produce that number. For example, the prime factors of 2001 are 3, 23 and 29, since 3×23×29 =
2001. Likewise, the prime factors of 200 are 2 and 5, since 23×52=200.
Write an algorithm to compute the prime factors of a number. Code your algorithm in a C
program. Now count the number of iterations through the loop required to factor a number. Is
there a relationship between the size of a number and the number of iterations required to factor it?
Prime factor analysis can be accelerated if only primes are used as trial divisors. Devise an
algorithm for finding the next prime after a given number and incorporate the algorithm into your
existing program. Does the performance improve? Why, or why not?
Additional Exercises
Brookshear: Chapter 4, Review Problems 14, 16, 17, 25
Goldschlager & Lister: Chapter 2, Questions 7, 9 and 10
Miscellaneous
Deitel & Deitel, Chapter 5, Exercises: 5.26, 5.27 and 5.28
T9: Lists (Search and Manipulation)
Page 2 of 2
Download