CS 162 - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CS 162

Description:

(Can be) Among the fastest sorting algorithms (but poor worst case time) Discovered by C.A.R. Hoare in 196X. A nice recursive algorithm ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 20
Provided by: osue6
Category:
Tags: hoare

less

Transcript and Presenter's Notes

Title: CS 162


1
CS 162
  • Fast Sorting
  • Quick Sort

2
Quick Sort
  • (Can be) Among the fastest sorting algorithms
    (but poor worst case time)
  • Discovered by C.A.R. Hoare in 196X
  • A nice recursive algorithm
  • Unlike merge sort, doesnt require additional
    memory

3
Quick sort vs merge sort
  • Similar basic concept
  • To sort an array of N elements, break apart,
    recursively sort, then put back together.
  • Merge sort spends more time on the put back part,
    quick sort on breaking apart

4
Break, Sort, Combine
Break
Sort Recursively
Combine
5
Breaking Apart
  • For merge sort, breaking apart is easy - just
    take half the elements
  • For quick sort, break by forming a partition
    around a pivot
  • Divide into elements smaller than or equal to
    pivot, elements larger than or equal to pivot

6
Partitioning around Pivot 7
5
9
2
4
7
8
6
12
Partition
5
2
4
6
7
9
8
12
Sort
8
9
12
2
4
5
6
7
7
Combining is easy
  • Now the combination step is easy - values are
    already in their correct location
  • But it is the partition step that is more complex
  • Problem - how do you guarantee the two parts are
    equal in size?

8
Quick Sort Algorithm
  • void quickSort (double storage)
  • quickSort (storage, 0, storage.length-1)
  • void quickSort (double storage, int low, int
    high)
  • if (low gt high) return // base case
  • int pivot (low high)/2 // one of many
    techniques
  • pivot partition(storage, low, high, pivot)
  • quickSort (storage, low, pivot-1)
  • quickSort (storage, pivot1, high)

9
How fast is quick sort?
  • If we are very lucky, each partition is equal in
    size, nice O(n log n) behavior like merge sort
  • If we are very Unlucky, one partition is empty,
    and the other is just one element smaller than
    the original

10
Picture of bad luck
N
(N-1)
(N-2)

1 N (N1)/2 which is O(n2) (not good!)
11
Are you more often lucky or unlucky?
  • So everything depends upon the choice of a good
    pivot
  • Are you more often lucky or unlucky?
  • Will come back to this topic after examining the
    partition algorithm

12
Description of partition algorithm
  • int partition(double data,
  • int low, int high, int
    pivot)
  • A. swap pivot and low
  • B. start i low1, j high, and move towards
    each other
  • C. when they meet, move pivot back to middle
    and return

13
Partition
Pivot
5
9
2
4
7
8
6
12
7
9
2
4
5
8
6
12
j
i
14
Inner Loop
  • while (i lt j)
  • if (datai lt pivot) increment i
  • else if dataj gt pivot) decrement j
  • else swap values at positions i and j

Pivot
lt Pivot
Unknown
gt Pivot
i
j
15
Once they meet
  • Values less than i are smaller than equal to
    pivot. Exchange pivot with position (i-1), and
    return i-1

Pivot
lt Pivot
gt Pivot
i
j
16
Picture at end of partition
  • Picture after final swap, right before return.
    Pivot is found at position (i-1)

Pivot
lt Pivot
gt Pivot
i
17
Rules for selecting pivot
  • So running time depends upon finding good pivot.
    Lots of possible rules
  • Select midpoint (what we do)
  • Select first element (bad if already sorted)
  • Select a random element
  • Select median of three values

18
Good points
  • Bad behavior does not arise very often
  • Unlike merge sort, does not require any
    additional memory

19
Now your turn
  • Questions?
  • If not, complete implementation of partition on
    worksheet
Write a Comment
User Comments (0)
About PowerShow.com