Docsity

CS61B Lecture #27: Sorting Algorithms - Quicksort, Selection, Counting, Radix Sorts, Slides of Data Structures and Algorithms

Toggle sidebar
bg1
CS61B Lecture #27
Today: Sorting, continued
•Quicksort
•Selection
•Distribution counting
•Radix sorts
Next topic readings:
Data Structures
, Chapter 9.
Public Service Announcement: Residential Computing is hiring. Be an
RCC and get paid for your computer skills. Flexible Hours, Work Study:
$11.97/hour Past RCC’s have gone on to places like Google, Apple, Mi-
crosoft and eBay. For more information visit:
http://rescomp.berkeley.edu/rcchiring.
Last modified: Wed Mar 22 17:16:35 2006 CS61B: Lecture #27 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download CS61B Lecture #27: Sorting Algorithms - Quicksort, Selection, Counting, Radix Sorts and more Slides Data Structures and Algorithms in PDF only on Docsity!

CS61B Lecture

Today: Sorting, continued

  • Quicksort
  • Selection
  • Distribution counting
  • Radix sorts

Next topic readings: Data Structures, Chapter 9.

Public Service Announcement: Residential Computing is hiring. Be an

RCC and get paid for your computer skills. Flexible Hours, Work Study:

$11.97/hour Past RCC’s have gone on to places like Google, Apple, Mi-

crosoft and eBay. For more information visit:

http://rescomp.berkeley.edu/rcchiring.

Quicksort: Speed through Probability

Idea:

  • Partition data into pieces: everything > a pivot value at the high end of the sequence to be sorted, and everything ≤ on the low end.
  • Repeat recursively on the high and low pieces.
  • For speed, stop when pieces are “small enough” and do insertion sort on the whole thing.
  • Reason: insertion sort has low constant factors. By design, no item will move out of its will move out of its piece [why?], so when pieces are small, #inversions is, too.
  • Have to choose pivot well. E.g.: median of first, last and middle items of sequence.

Performance of Quicksort

  • Probabalistic time:
    • If choice of pivots good, divide data in two each time: Θ(N lg N ) with a good constant factor relative to merge or heap sort.
    • If choice of pivots bad, most items on one side each time: Θ(N 2 ).
    • Ω(N lg N ) in best case, so insertion sort better for nearly or- dered input sets.
  • Interesting point: randomly shuffling the data before sorting makes

Ω(N 2 ) time very unlikely!

Quick Selection

The Selection Problem: for given k, find kth smallest element in data.

  • Obvious method: sort, select element #k, time Θ(N lg N ).
  • If k ≤ some constant, can easily do in Θ(N ) time:
    • Go through array, keep smallest k items.
  • Get probably Θ(N ) time for all k by adapting quicksort:
    • Partition around some pivot, p, as in quicksort, arrange that pivot ends up at dividing line.
    • Suppose that in the result, pivot is at index m, all elements ≤ pivot have indicies ≤ m.
    • If m = k, you’re done: p is answer.
    • If m > k, recursively select kth from left half of sequence.
    • If m < k, recursively select (k − m − 1)th from right half of sequence.

Selection Performance

  • For this algorithm, if m roughly in middle each time, cost is

C(N ) =

  

1 , if N = 1, N + C(N/2), otherwise. = N + N/2 +... + 1

= 2N − 1 ∈ Θ(N )

  • But in worst case, get Θ(N 2 ), as for quicksort.
  • By another, non-obvious algorithm, can get Θ(N ) worst-case time

for all k (take CS170).

Better than N lg N?

  • Can prove that if all you can do to keys is compare them then sorting

must take Ω(N lg N ).

  • Basic idea: there are N! possible ways the input data could be

scrambled.

  • Therefore, your program must be prepared to do N! different com-

binations of move operations.

  • Therefore, there must be N! possible combinations of outcomes of

all the if tests in your program (we’re assuming that comparisons are 2-way).

  • Since each if test goes two ways, number of possible different out-

comes for k if tests is 2 k.

  • Thus, need enough tests so that 2 k^ > N !, which means k ∈ Ω(lg N !).
  • Using Stirling’s approximation,

m! ∈

2 πm

  m

e

 

m  1 + Θ

 ^1 m

 

  (^) ,

this tells us that k ∈ Ω(N lg N ).

Distribution Counting Example

  • Suppose all items are between 0 and 9 as in this example:

7 0 4 0 9 1 9 1 9 5 3 7 3 1 6 7 4 2 0

3 0

3 1

1 2

2 3

2 4

1 5

1 6

3 7

0 8

3 9

Counts

0 < 0

3 < 1

6 < 2

7 < 3

9 < 4

11 < 5

12 < 6

13 < 7

16 < 8

16 < 9

Running sum

0 0

0 0 1 3

1 1 2 6

3 3 4 9

4 5 11

6 12

7 13

7 7 9 16

9 9

  • “Counts” line gives # occurrences of each key.
  • “Running sum” gives cumulative count of keys ≤ each value... -... which tells us where to put each key:
  • The first instance of key k goes into slot m, where m is the number

of key instances that are < k.

Radix Sort

Idea: Sort keys one character at a time.

  • Can use distribution counting for each digit.
  • Can work either right to left (LSD radix sort) or left to right (MSD radix sort)
  • LSD radix sort is venerable: used for punched cards.

Initial: set, cat, cad, con, bat, can, be, let, bet

be ‘t’

cad ‘d’

can con ‘n’

bet let bat cat set ‘t’

Pass 1 (by char #2)

be, cad, con, can, set, cat, bat, let, bet

bat cat can cad ‘a’

bet let set be ‘e’

con ‘o’

Pass 2 (by char #1)

cad, can, cat, bat, be, set, let, bet, con

bet be bat ‘b’

con cat can cad ‘c’

let ‘l’

set ‘s’

Pass 3 (by char #0)

bat, be, bet, cad, can, cat, con, let, set

Performance of Radix Sort

  • Radix sort takes Θ(B) time where B is total size of the key data.
  • Have measured other sorts as function of #records.
  • How to compare?
  • To have N different records, must have keys at least Θ(lg N ) long

[why?]

  • Furthermore, comparison actually takes time Θ(K) where K is size

of key in worst case [why?]

  • So N lg N comparisons really means N (lg N )^2 operations.
  • While radix sort takes B = N lg N time.
  • On the other hand, must work to get good constant factors with

radix sort.

And Don’t Forget Search Trees

Idea: A search tree is in sorted order, when read in inorder.

  • Need balance to really use for sorting [next topic].
  • Given balance, same performance as heapsort: N insertions in time lg N each, plus Θ(N ) to traverse, gives

Θ(N + N lg N ) = Θ(N lg N )