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. 7.2 Three Θ(n 2 ) Sorting <strong>Algorithm</strong>s 243Insertion Bubble SelectionComparisons:Best Case Θ(n) Θ(n 2 ) Θ(n 2 )Average Case Θ(n 2 ) Θ(n 2 ) Θ(n 2 )Worst Case Θ(n 2 ) Θ(n 2 ) Θ(n 2 )Swaps:Best Case 0 0 Θ(n)Average Case Θ(n 2 ) Θ(n 2 ) Θ(n)Worst Case Θ(n 2 ) Θ(n 2 ) Θ(n)Figure 7.5 A comparison of the asymp<strong>to</strong>tic complexities for three simple sortingalgorithms.7.2.4 The Cost of Exchange SortingFigure 7.5 summarizes the cost of Insertion, Bubble, <strong>and</strong> Selection Sort in terms oftheir required number of comparisons <strong>and</strong> swaps 1 in the best, average, <strong>and</strong> worstcases. The running time for each of these sorts is Θ(n 2 ) in the average <strong>and</strong> worstcases.The remaining sorting algorithms presented in this chapter are significantly betterthan these three under typical conditions. But before continuing on, it is instructive<strong>to</strong> investigate what makes these three sorts so slow. The crucial bottleneckis that only adjacent records are compared. Thus, comparisons <strong>and</strong> moves (in allbut Selection Sort) are by single steps. Swapping adjacent records is called an exchange.Thus, these sorts are sometimes referred <strong>to</strong> as exchange sorts. The cos<strong>to</strong>f any exchange sort can be at best the <strong>to</strong>tal number of steps that the records in thearray must move <strong>to</strong> reach their “correct” location (i.e., the number of inversions foreach record).What is the average number of inversions? Consider a list L containing n values.Define L R <strong>to</strong> be L in reverse. L has n(n−1)/2 distinct pairs of values, each ofwhich could potentially be an inversion. Each such pair must either be an inversionin L or in L R . Thus, the <strong>to</strong>tal number of inversions in L <strong>and</strong> L R <strong>to</strong>gether is exactlyn(n − 1)/2 for an average of n(n − 1)/4 per list. We therefore know with certaintythat any sorting algorithm which limits comparisons <strong>to</strong> adjacent items will cost atleast n(n − 1)/4 = Ω(n 2 ) in the average case.1 There is a slight anomaly with Selection Sort. The supposed advantage for Selection Sort is itslow number of swaps required, yet Selection Sort’s best-case number of swaps is worse than that forInsertion Sort or Bubble Sort. This is because the implementation given for Selection Sort does notavoid a swap in the case where record i is already in position i. The reason is that it usually takesmore time <strong>to</strong> repeatedly check for this situation than would be saved by avoiding such swaps.

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

Saved successfully!

Ooh no, something went wrong!