People who like this sort of thing will find this the sort of thing they like' - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

People who like this sort of thing will find this the sort of thing they like'

Description:

... who like this sort of thing will find this the sort of thing they like. Abraham Lincoln (1809 - 1865), in a book review. CSE 502N. Fundamentals of Computer Science ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 8
Provided by: davidedwa
Category:
Tags: book | find | people | review | sort | thing

less

Transcript and Presenter's Notes

Title: People who like this sort of thing will find this the sort of thing they like'


1
  • People who like this sort of thing will find this
    the sort of thing they like.
  • Abraham Lincoln (1809 - 1865), in a book
    review

2
CSE 502NFundamentals of Computer Science
  • Fall 2004
  • Lecture 8
  • Quicksort, randomized quicksort,
  • running time analysis
  • (CLRS 7.1-7.4)

3
Quicksort
  • Sorts array A of n numbers in-place
  • Divide partition rearrange Apr into
    subarrays around a pivot point q such that each
    element in Apq-1 Aq each element in
    Aq1r
  • Conquer sort subarrays and via recursive calls
  • Combine since subarrays are sorted in place,
    entire array is Apr sorted

Quicksort(A,p,r) q Partition(A,p,r)
Quicksort(A,p,q-1) Quicksort(A,q1,r)
Partition(A,p,r) i p-1 x Ar for j
p to r-1 if Aj x then i
exchange Ai Aj exchange
Ai1 Ar return i1
4
Correctness of Quicksort
  • Loop invariant (for loop in Partition)
  • If p k i, then Ak x
  • If i1 k j-1, then Ak gt x
  • If k r, then Ak x
  • Initialization prior to first iteration, i p-1
    and j p
  • There are no values between p and i (condition 1)
  • There are no values between i1 and j-1
    (condition 2)
  • Third condition satisfied by initialization of x
  • Maintenance two cases depending on outcome of if
    compare
  • If Aj gt x, j is incremented, condition 2 holds,
    no other entries affected
  • If Aj x, i is incremented, Ai and Aj are
    swapped, j is incremented
  • Ai x, (condition 1)
  • Aj-1 gt x, (condition 2, by loop invariant)

5
Correctness of Quicksort
  • Termination at termination j r, and all
    entries have been partitioned into one of the
    three sets described by loop invariant
  • If p k i, then Ak x
  • If i1 k j-1, then Ak gt x
  • If k r, then Ak x
  • Recursive calls operate on regions of at most n-1
    elements
  • Api where all elements are Ai1
  • Ai2r where all elements are gt Ai1
  • Running time of Partition is Q(n) where n r p
    1

6
Quicksort Performance
  • Performance depends on whether or not the
    partitioning is balanced or unbalanced
  • If unbalanced, worst-case running time is no
    better than insertion sort
  • T(n) T(n 1) T(0) Q(n) T(n 1) Q(n)
    Q(n2)
  • Occurs when array is already sorted when
    insertion sort is O(n)
  • If evenly balanced, running time is equal to
    merge sort
  • T(n) 2T(n/2) Q(n) O(n lg n)
  • How balanced does the partitioning need to be to
    achieve O(n lg n)?
  • How probable is the balanced or unbalanced case?

7
Randomized Quicksort
  • Sorts array A of n numbers in-place
  • Randomly chooses pivot point to yield reasonably
    balanced partitions on average

Randomized-Quicksort(A,p,r) q
Randomized-Partition(A,p,r) Randomized-Quicksor
t(A,p,q-1) Randomized-Quicksort(A,q1,r)
Randomized-Partition(A,p,r) i Random(p,r)
exchange Ar Ai return Partition(A,p,r)
Write a Comment
User Comments (0)
About PowerShow.com