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...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

24 Chap. 1 <strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Algorithm</strong>s1.18 Imagine that you are a programmer who must write a function <strong>to</strong> sort anarray of about 1000 integers from lowest value <strong>to</strong> highest value. Write downat least five approaches <strong>to</strong> sorting the array. Do not write algorithms in Javaor pseudocode. Just write a sentence or two for each approach <strong>to</strong> describehow it would work.1.19 Think of an algorithm <strong>to</strong> find the maximum value in an (unsorted) array.Now, think of an algorithm <strong>to</strong> find the second largest value in the array.Which is harder <strong>to</strong> implement? Which takes more time <strong>to</strong> run (as measuredby the number of comparisons performed)? Now, think of an algorithm <strong>to</strong>find the third largest value. Finally, think of an algorithm <strong>to</strong> find the middlevalue. Which is the most difficult of these problems <strong>to</strong> solve?1.20 An unsorted list of integers allows for constant-time insert simply by addinga new integer at the end of the list. Unfortunately, searching for the integerwith key value X requires a sequential search through the unsorted list untilyou find X, which on average requires looking at half the list. On the otherh<strong>and</strong>, a sorted array-based list of n integers can be searched in log n time byusing a binary search. Unfortunately, inserting a new integer requires a lot oftime because many integers might be shifted in the array if we want <strong>to</strong> keepit sorted. How might data be organized <strong>to</strong> support both insertion <strong>and</strong> searchin log n time?

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

Saved successfully!

Ooh no, something went wrong!