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.

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 intuitivestrategies that we can use <strong>to</strong> do the sorting, depending on how many objects wehave <strong>to</strong> sort <strong>and</strong> how hard they are <strong>to</strong> move around. Sorting is also one of the mostfrequently performed computing tasks. We might sort the records in a databaseso that we can search the collection efficiently. We might sort the records by zipcode so that we can print <strong>and</strong> mail them more cheaply. We might use sorting as anintrinsic part of an algorithm <strong>to</strong> solve some other problem, such as when computingthe minimum-cost spanning tree (see Section 11.5).Because sorting is so important, naturally it has been studied intensively <strong>and</strong>many algorithms have been devised. Some of these algorithms are straightforwardadaptations of schemes we use in everyday life. Others are <strong>to</strong>tally alien <strong>to</strong> how humansdo things, having been invented <strong>to</strong> sort thous<strong>and</strong>s or even millions of recordss<strong>to</strong>red on the computer. After years of study, there are still unsolved problemsrelated <strong>to</strong> sorting. New algorithms are still being developed <strong>and</strong> refined for specialpurposeapplications.While introducing this central problem in computer science, this chapter hasa secondary purpose of illustrating many important issues in algorithm design <strong>and</strong>analysis. The collection of sorting algorithms presented will illustrate that divide<strong>and</strong>-conqueris a powerful approach <strong>to</strong> solving a problem, <strong>and</strong> that there are multipleways <strong>to</strong> do the dividing. Mergesort divides a list in half. Quicksort divides a listin<strong>to</strong> big values <strong>and</strong> small values. And Radix Sort divides the problem by workingon one digit of the key at a time.Sorting algorithms will be used <strong>to</strong> illustrate a wide variety of analysis techniquesin this chapter. We’ll find that it is possible for an algorithm <strong>to</strong> have anaverage case whose growth rate is significantly smaller than its worse case (Quicksort).We’ll see how it is possible <strong>to</strong> speed up sorting algorithms (both Shellsort235

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

Saved successfully!

Ooh no, something went wrong!