0% found this document useful (0 votes)
3 views

4-quicksort

Uploaded by

Monika Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

4-quicksort

Uploaded by

Monika Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Program: B.

Tech
Course Code: AIML303/AIDS303/IOT303
Course Name: Design & Analysis of Algorithms

Quick Sort

By:
Dr. Monika Bansal
Quick Sort
Quicksort
Partition

…5 7 1 2 8 4 3 6…

p r
i
…5 7 1 2 8 4 3 6…

p r
i j
…5 7 1 2 8 4 3 6…

p r
i j
…5 7 1 2 8 4 3 6…

p r
ij
…5 7 1 2 8 4 3 6…

p r
ij
…5 7 1 2 8 4 3 6…

p r
i j
…5 7 1 2 8 4 3 6…

p r
i j
…5 7 1 2 8 4 3 6…

p r
i j
…5 7 1 2 8 4 3 6…

p r
i j
…5 1 7 2 8 4 3 6…

p r
i j
…5 1 7 2 8 4 3 6…

p r
i j
…5 1 7 2 8 4 3 6…

p r
i j
…5 1 2 7 8 4 3 6…

p r
i j
…5 1 2 7 8 4 3 6…

p r
i j
…5 1 2 7 8 4 3 6…

p r

What’s happening?
i j
…5 1 2 7 8 4 3 6…

p r

≤ pivot > pivot unprocessed


i j
…5 1 2 7 8 4 3 6…

p r
i j
…5 1 2 4 8 7 3 6…

p r
i j
…5 1 2 4 3 7 8 6…

p r
i j
…5 1 2 4 3 6 8 7…

p r
i j
…5 1 2 4 3 6 8 7…

p r
Partition
Partitions the elements A[p…r-1] in to two sets, those ≤ pivot and
those > pivot
Partition running time?

Θ(n)
Quicksort
8 5 1 3 6 2 7 4
8 5 1 3 6 2 7 4
1 3 2 4 6 8 7 5
1 3 2 4 6 8 7 5
1 3 2 4 6 8 7 5
1 2 3 4 6 8 7 5
1 2 3 4 6 8 7 5
1 2 3 4 6 8 7 5
1 2 3 4 6 8 7 5
1 2 3 4 6 8 7 5
1 2 3 4 5 8 7 6

What happens here?


1 2 3 4 5 8 7 6
1 2 3 4 5 8 7 6
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
Some observations

Divide and conquer: different than MergeSort – do the


work before recursing

How many times is/can an element selected for as a


pivot?

What happens after an element is selected as a pivot?

1 3 2 4 6 8 7 5
Running time of Quicksort?

Worst case?

Each call to Partition splits the array into an empty array and n-1
array
Quicksort: Worse case running time

T (n) = T (n − 1) + (n)

Which is? Θ(n2)

When does this happen?


⚫ sorted

⚫ reverse sorted

⚫ near sorted/reverse sorted


Quicksort best case?

Each call to Partition splits the array into two equal parts

T (n) = 2T (n / 2) + (n)
O(n log n) (same as merge sort)

When does this happen?


⚫ random data?
Average-case analysis
Let the partition procedure always splits the
array into some constant ratio b-to-a, e.g. 9-to-1
 a   b 
T ( n)  T  n + T n  + cn
 a+b   a+b 
e.g.
T (n) = T (101 n)+ T (109 n)+ (n)

What is the solution to this recurrence?


Average-case analysis
T (n)
Average-case analysis
cn
T (101 n) T (109 n)
Average-case analysis
cn
1
10
cn 9
10
cn

T (100
1
n)T (100
9
n) T (100
9
n)T (100
81
n)
Average-case analysis
cn cn
1
10
cn 9
10
cn cn
log10/9 n
1 9 9 81 cn
100
cn 100
cn 100
cn 100
cn


(1) O(n) leaves
(1)
Average-case analysis
cn cn
1
10
cn 9
10
cn cn
log10 log10/9 n
n 1 cn 9
cn 9
cn 81 cn
cn
100 100 100 100


(1) O(n) leaves

(n lg n) (1)
cn log10n  T(n)  cn log10/9n + (n)
Average-case analysis
Randomized quicksort

IDEA: Partition around a random element.


•Running time is independent of the input
order.
•No assumptions need to be made about the
input distribution.
•No specific input elicits the worst-case
behavior.
•The worst case is determined only by the
output of a random-number generator.
Randomized quicksort

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy