12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 3.14 Projects 95a summation for the running time of your algorithm on n pieces, <strong>and</strong> thenderive a closed-form solution for the summation.3.22 Can the average case cost for an algorithm be worse than the worst case cost?Can it be better than the best case cost? Explain why or why not.3.23 Prove that if an algorithm is Θ(f(n)) in the average case, then it is Ω(f(n))in the worst case.3.24 Prove that if an algorithm is Θ(f(n)) in the average case, then it is O(f(n))in the best case.3.14 Projects3.1 Imagine that you are trying <strong>to</strong> s<strong>to</strong>re 32 boolean values, <strong>and</strong> must access themfrequently. Compare the time required <strong>to</strong> access Boolean values s<strong>to</strong>red alternativelyas a single bit field, a character, a short integer, or a long integer.There are two things <strong>to</strong> be careful of when writing your program. First, besure that your program does enough variable accesses <strong>to</strong> make meaningfulmeasurements. A single access is much smaller than the measurement ratefor all four methods. Second, be sure that your program spends as much timeas possible doing variable accesses rather than other things such as callingtiming functions or incrementing for loop counters.3.2 Implement sequential search <strong>and</strong> binary search algorithms on your computer.Run timings for each algorithm on arrays of size n = 10 i for i ranging from1 <strong>to</strong> as large a value as your computer’s memory <strong>and</strong> compiler will allow. Forboth algorithms, s<strong>to</strong>re the values 0 through n − 1 in order in the array, <strong>and</strong>use a variety of r<strong>and</strong>om search values in the range 0 <strong>to</strong> n − 1 on each sizen. Graph the resulting times. When is sequential search faster than binarysearch for a sorted array?3.3 Implement a program that runs <strong>and</strong> gives timings for the two Fibonacci sequencefunctions provided in Exercise 2.11. Graph the resulting runningtimes for as many values of n as your computer can h<strong>and</strong>le.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!