11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

7Internal SortingWe sort many things in our everyday lives: A h<strong>and</strong>ful of cards when playing Bridge;bills <strong>and</strong> other piles of paper; jars of spices; <strong>and</strong> so on. And we have many intuitivestr<strong>at</strong>egies th<strong>at</strong> we can use to do the sorting, depending on how many objects wehave to sort <strong>and</strong> how hard they are to move around. Sorting is also one of the mostfrequently performed computing tasks. We might sort the records in a d<strong>at</strong>abaseso th<strong>at</strong> we can search the collection efficiently. We might sort the records by zipcode so th<strong>at</strong> we can print <strong>and</strong> mail them more cheaply. We might use sorting as anintrinsic part of an algorithm to solve some other problem, such as when computingthe minimum-cost spanning tree (see Section 11.5).Because sorting is so important, n<strong>at</strong>urally it has been studied intensively <strong>and</strong>many algorithms have been devised. Some of these algorithms are straightforwardadapt<strong>at</strong>ions of schemes we use in everyday life. Others are totally alien to how humansdo things, having been invented to sort thous<strong>and</strong>s or even millions of recordsstored on the computer. After years of study, there are still unsolved problemsrel<strong>at</strong>ed to sorting. New algorithms are still being developed <strong>and</strong> refined for specialpurposeapplic<strong>at</strong>ions.While introducing this central problem in computer science, this chapter hasa secondary purpose of illustr<strong>at</strong>ing issues in algorithm design <strong>and</strong> analysis. Forexample, this collection of sorting algorithms shows multiple approaches to usingdivide-<strong>and</strong>-conquer. In particular, there are multiple ways to do the dividing:Mergesort divides a list in half; Quicksort divides a list into big values <strong>and</strong> smallvalues; <strong>and</strong> Radix Sort divides the problem by working on one digit of the key <strong>at</strong>a time. Sorting algorithms can also illustr<strong>at</strong>e a wide variety of analysis techniques.We’ll find th<strong>at</strong> it is possible for an algorithm to have an average case whose growthr<strong>at</strong>e is significantly smaller than its worse case (Quicksort). We’ll see how it ispossible to speed up sorting algorithms (both Shellsort <strong>and</strong> Quicksort) by takingadvantage of the best case behavior of another algorithm (Insertion sort). We’ll seeseveral examples of how we can tune an algorithm for better performance. We’llsee th<strong>at</strong> special case behavior by some algorithms makes them a good solution for223

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

Saved successfully!

Ooh no, something went wrong!