Docsity

Mergesort and Quicksort Algorithms, Slides of Data Structures and Algorithms

Partial preview of the text

Download Mergesort and Quicksort Algorithms and more Slides Data Structures and Algorithms in PDF only on Docsity!

CPSC 223

Algorithms & Data Abstract Structures

Lecture 12:

More on Sorting – Mergesort and

Quicksort

Today …

• Continuing with Sorting

• Mergesort [Sect. 9.2, pp. 466-472]

• Quicksort [Sect. 9.2, pp. 472-484]

CPSC 223 -­‐-­‐ Fall 2010 2

Mergesort

CPSC 223 -­‐-­‐ Fall 2010 3

Mergesort [von Neumann, 1945]

  • A “divide and conquer” approach
  • The basic idea:
    • Divide the list into two halves
    • Sort each half
    • Merge the sorted halves … Merging is fast when the two sublists are sorted CPSC 223 -­‐-­‐ Fall 2010 4 29 10 14 13 29 10 14 13 Ini3al list: ( n =4) 29 10 14 13 Split Sort^ Merge 10 29 13 14 10 29 13 14 10 13 14 29

Mergesort (based on textbook)

void Mergesort (Entry theArray[], int n, int first, int last) { if(first < last) { int mid = (first + last) / 2; Mergesort (theArray, n, first, mid); Mergesort (theArray, n, mid + 1, last); Merge (theArray, n, first, mid, last); } } CPSC 223 -­‐-­‐ Fall 2010 7

Mergesort (based on textbook)

void Merge (Entry theArray[], int n, int first, int mid, int last) { Entry tmpArray[n]; int first1 = first, first2 = mid + 1, i = first1; for(; first1 <= mid && first2 <= last; i++) { if(theArray[first1] < theArray[first2]) tmpArray[i] = theArray[first1++]; else tmpArray[i] = theArray[first2++]; } for(; first1 <= mid; i++, first1++) // copy remaining from first half tmpArray[i] = theArray[first1]; for(; first2 <= last; i++, first2++) // copy remaining from sec. half tmpArray[i] = theArray[first2]; for(int j = first; j <= last; j++) // copy tmpArray to the array theArray[j] = tmpArray[j]; } CPSC 223 -­‐-­‐ Fall 2010 8

Mergesort

The merge step

  • Assume number of elements to be merged is n
    • n = length of first half + length of second half
  • The merge step requires
    • n – 1 comparisons (worst case)
    • n moves from the original array to the temp array
    • n moves from the temp array to the original array
  • So merge costs 3 n – 1
  • Which is O ( n ) CPSC 223 -­‐-­‐ Fall 2010 9

Mergesort

  • How expensive is mergesort?
    • Each time we call mergesort twice (halving the list)
    • Assume n items in the list
    • The recursion goes approximately log 2 n levels deep
    • At each level the merges cost a total of O ( n )
  • So mergesort is O ( n log n ) !!! CPSC 223 -­‐-­‐ Fall 2010 10 29 10 14 13 Ini3al list ( n =4) 29 10 14 13 Split (^29 10 14 13) Split 2 levels of recursion

Comparison of Sorting Algorithms

CPSC 223 -­‐-­‐ Fall 2010 13 Selec3on Sort Bubble Sort Best Case Average Case Worst Case O ( n^2 ) O ( n^2 ) O ( n^2 ) O ( n ) O ( n^2 ) O ( n^2 ) Inser3on Sort O ( n ) O ( n^2 ) O ( n^2 ) Quicksort Heapsort Treesort Mergesort O ( n log n ) O ( n log n ) O ( n log n )

Quicksort

CPSC 223 -­‐-­‐ Fall 2010 14

Quicksort [Hoare, 1962]

  • Another “ divide and conquer ” approach
  • The basic idea:
    • Pick a “ pivot ” element in the list
    • Put values smaller than the pivot on the left
    • Put values larger than the pivot on the right
    • Put pivot value in its final sorted position
    • Repeat on the left and right sublists CPSC 223 -­‐-­‐ Fall 2010 15 29 10 32 13 29 10 32 13 Ini3al list: ( n =4) 13 10 29 32 Pick pivot Par33on and Place Pivot^ Repeat on^ Sublists 29 10 13 32 13 10 32 P1 P 29 10 13 29 32

Quicksort

CPSC 223 -­‐-­‐ Fall 2010 16 29 10 32 13 Ini3al list ( n =4) Par22on 13 10 29 32 n = 2, n = 1 Par22on^ pivot 10 13 n^ = 1,^ n^ = 0 pivot

Quicksort

Partitioning step (where the work is done)

  • Worst case
    • When pivot is smallest (largest) element
    • We end up with only one partition
    • This partition is of size n – 1
  • In general, the partition step has O ( n ) comparisons

and swaps

  • We do this partitioning n – 1 times in the worst case
  • Therefore quicksort is O ( n^2 ) in the worst case CPSC 223 -­‐-­‐ Fall 2010 19

Quicksort

Quicksort is much better in practice

  • Each recursive step on avg. partitions array into equal halves
  • So we have approx. log 2 n recursion “levels” … O (log n )
  • At each level we have O ( n ) comparisons and swaps
  • So quicksort is O ( n log n ) in the average (best) case Note that compared to Mergesort …
  • Quicksort does not have a “merge” step
  • Each level has a length n -1 array
  • Often makes quicksort fast in practice
  • Also can spend some time picking a “good” pivot CPSC 223 -­‐-­‐ Fall 2010 20

Comparison of Sorting Algorithms

CPSC 223 -­‐-­‐ Fall 2010 21 Selec3on Sort Bubble Sort Best Case Average Case Worst Case O ( n^2 ) O ( n^2 ) O ( n^2 ) O ( n ) O ( n^2 ) O ( n^2 ) Inser3on Sort O ( n ) O ( n^2 ) O ( n^2 ) Quicksort O ( n log n ) O ( n log n ) O ( n^2 ) Heapsort Treesort Mergesort O ( n log n ) O ( n log n ) O ( n log n )