Quicksort - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Quicksort

Description:

Constants in the (n lg n) are small, so it's often the best ... Everything between p and i is x. Everything between i and j -1 x. 2. 8. 7. 1. 3. 5. 6. 4 ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 20
Provided by: jeffch8
Category:
Tags: pandi | quicksort

less

Transcript and Presenter's Notes

Title: Quicksort


1
Quicksort
2
Properties of QuickSort
  • Runs in ?(n2) worst case, but ?(n lg n) on
    average
  • Constants in the ?(n lg n) are small, so its
    often the best practical sorting algorithm
  • Sorts in place
  • Several variations
  • randomized partitioning
  • random sampling

3
Overview of QuickSort
  • Divide The array p..r is partitioned into two
    non-empty subarrays Ap..q and Aq1 .. r such
    that each element in the first array is less than
    or equal to each item in the second
  • Conquer Sort recursively
  • Combine Since they are already sorted

4
QuickSort
  • QUICKSORT (A, p, r)
  • if p lt r
  • then q ? PARTITION (A, p, r)
  • QUICKSORT (A, p, q)
  • QUICKSORT (A, q1, r)
  • Note obviously, PARTITION is the key here!

5
Partitioning
  • PARTITION selects a pivot element for
    partitioning
  • We have a range p to r in A with two variables i
    and j, and a pivot point x, initialized to r.
  • At the beginning of each loop
  • Everything between p and i is ? x
  • Everything between i and j -1 gt x

6
i
p,j
r
2
8
7
1
3
5
6
4
7
p,i
r
j
2
8
7
1
3
5
6
4
8
p,i
r
j
2
8
7
1
3
5
6
4
9
p,i
r
j
2
8
7
1
3
5
6
4
10
p
r
j
i
2
1
7
8
3
5
6
4
11
p
r
j
i
2
1
3
8
7
5
6
4
12
p
r
j
i
2
1
3
8
7
5
6
4
13
p
r
i
2
1
3
8
7
5
6
4
14
p
r
i
2
1
3
4
7
5
6
8
This is in the correct spot!
15
PARTITION
  • PARTITION (A, p, r)
  • 1 x ? Ar
  • 2 i ? p - 1
  • 3 for j ? p to r 1
  • 4 do if Aj ? x
  • 5 then i ? i 1
  • 6 exchange Ai ? Aj
  • 7 exchange Ai 1 ? Ar
  • 8 return i 1

16
Performance
  • Depends heavily on whether the partitioning is
    balanced
  • Unbalanced can run as slow as insertion sort!
  • Worst-case two subproblems with
  • n 1 elements
  • 0 elements
  • T (n) T (n 1) T (0) ?(n)
  • Decomposes to ?(n2)

17
Whats really going on...
  • Average time not close to that...
  • Suppose that we have a 9-to-1 split
  • T (n) T (9n/10) T (n/10) cn
  • Then, a subproblem at depth i is 9in/10i
  • Solving for i yields a tree of depth log10/9 n
  • Thus, the height of the tree is still
    logarithmic!
  • Even a 99-to-1 split is logarithmic
  • Any split of constant proportionality yields a
    tree of depth ?(lg n)
  • Note 80 of the time, PARTITION produces a split
    more balanced than 9-to-1

18
Randomized Quicksort
  • Uses random sampling
  • Use a randomly chosen element from the array
  • Swap Ar with the chosen element
  • RANDOMIZED-PARTITION (A, p, r)
  • 1 i ? RANDOM (p, r)
  • 2 exchange Ar ? Ai
  • 3 return PARTITION (A, p, r)

19
Summary
  • Runs in O(n log n) on average
  • Runs in O(n2) in worst-case
  • It uses partitioning to sort
  • Randomized partitioning helps
  • Quicksort is preferred, because of its low
    constant factors
Write a Comment
User Comments (0)
About PowerShow.com