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

set6_quicksort

Quicksort is a divide-and-conquer sorting algorithm that efficiently sorts an array by partitioning it into two subarrays and recursively sorting them. Its performance can vary based on the balance of partitioning, with best-case scenarios achieving speeds comparable to merge sort, while worst-case scenarios can degrade to insertion sort speeds. Randomized versions of quicksort help mitigate worst-case performance by selecting pivots randomly, ensuring consistent average performance regardless of input order.

Uploaded by

shai5ahalz3bi
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)
5 views

set6_quicksort

Quicksort is a divide-and-conquer sorting algorithm that efficiently sorts an array by partitioning it into two subarrays and recursively sorting them. Its performance can vary based on the balance of partitioning, with best-case scenarios achieving speeds comparable to merge sort, while worst-case scenarios can degrade to insertion sort speeds. Randomized versions of quicksort help mitigate worst-case performance by selecting pivots randomly, ensuring consistent average performance regardless of input order.

Uploaded by

shai5ahalz3bi
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/ 29

CpE 300

Design and Analysis


of Algorithms

QUICKSORT

1
Quicksort

• It is based on divide-and-conquer paradigm.


• Divide: The input array 𝐴[𝑝. . 𝑟] is divided into two nonempty
subarrays 𝐴[𝑝. . 𝑞] and 𝐴[𝑞 + 1. . 𝑟] such that
• each element in first subarray < each element in the second
subarray
• Conquer: The subarrays 𝐴[𝑝. . 𝑞] and 𝐴[𝑞 + 1. . 𝑟] are sorted by
recursive calls to quicksort.
• Combine: No work needed since subarrays are sorted in place.

2
Quicksort

• Quicksort is often the best practical choice for


sorting because it is remarkably efficient on the
average:

• It sorts in place.

• Works well in virtual environment.

3
Quicksort Algorithm

L x G

x
Example

A 2 8 7 1 3 5 6 4

Initial Call: 𝑄𝑈𝐼𝐶𝐾𝑆𝑂𝑅𝑇(𝐴, 1, 8)

5
𝑖 𝑝𝑗 𝑟

Partition Step1 2 8 7 1 3 5 6 4
𝑝 𝑖 𝑗 𝑟

Step2 2 8 7 1 3 5 6 4

𝑝 𝑖 𝑗 𝑟

Step3 2 8 7 1 3 5 6 4

𝑝 𝑖 𝑗 𝑟

Step4 2 8 7 1 3 5 6 4

6
𝑝 𝑖 𝑗 𝑟

2 1 7 8 3 5 6 4
Partition
Step5

𝑝 𝑖 𝑗 𝑟

Step6 2 1 3 8 7 5 6 4

𝑝 𝑖 𝑗 𝑟

Step7 2 1 3 8 7 5 6 4

𝑝 𝑖 𝑟

Step8 2 1 3 8 7 5 6 4
𝑝 𝑖 𝑟

Step9 2 1 3 4 7 5 6 8
7
Example

2 8 7 1 3 5 6 4

2 1 3 4 7 5 6 8

2 1 3 7 5 6 8

1 2 5 6 7
8
Performance of Quicksort

• The running time of quicksort depends on


whether the partitioning is balanced or
unbalanced,
• which in turn depends on which elements are used for
partitioning (pivot)

• If the partitioning is balanced, the algorithm runs


asymptotically as fast as merge sort.
• If the partitioning is unbalanced, however, it can
run asymptotically as slowly as insertion sort. 9
Worst Case Partitioning

• Happens when the input is sorted or reversely


sorted, which is commonly the case!
• The partitioning is around the maximum or
the minimum.
• In this case, one side of the partition always
has no element, the tree has the highest
height.

10
Worst Case
Partitioning 𝑛

0 𝑛-1

0 𝑛−2

Partition Complexity

11
Worst Case Partitioning

Height = 0 𝑛

Height 𝒏 Cost of level


0 𝑛 𝑛
Height = 1 0 𝑛−1
1 𝑛−1 𝑛−1
2 𝑛−2 𝑛−2
… … … Height = 2
0 𝑛−2
… … …
.
𝑘 𝑛−𝑘 =1 𝑛−𝑘 . .
.
Height = k?
𝑇(1)

𝑛−𝑘 =1→𝑘 =𝑛−1


12
Worst Case + +
Partitioning
𝑛(𝑛 + 1)
Height 𝒏 Cost of level 𝑘=
2
0 𝑛 𝑛
1 𝑛−1 𝑛−1 ( )
2 𝑛−2 𝑛−2
… … …
… … …
𝑘 𝑛−𝑘 =1 𝑛−𝑘

𝑛−𝑘 =1→𝑘 =𝑛−1


13
Best Case Partitioning

• Happens when each partitioning divides the array


to half
• each of size no more than , one is of size and

one of size − 1.

• In this case, the tree is balanced. 𝑛

𝑛 𝑛
−1
2 2
𝒏
𝟐
14
Best Case
Partitioning

Using case 2:

Then

15
Average Case Partitioning

• Any constant partitioning leads to the best case


performance.
• “Almost” Best-Case Analysis
• Average case closer to the best case than the
worst case.
• Example: Assume the partitioning is 1:9
• 9/10 of the elements are less than the pivot and 1/10
are greater than the pivot

16
Average
Shortest Path Longest Path
Case Ω Ο

Partitioning

. . . .
. . . .

17
Shortest Path

Height 𝒏 Cost of level


0 𝑛 𝑛 𝑛
Height = 0

1 𝑛 𝑛 9𝑛
10 + =𝑛
10 10
𝑛 9𝑛
2 𝑛 𝑛 9𝑛 9𝑛 81𝑛 Height = 1
+ + + =𝑛 10 10
100 100 100 100 100
… … …
… … … 𝑛 9𝑛 9𝑛 81𝑛
Height = 2 100 100 100 100
𝑘 𝑛 𝑛
=1
10 . . . . .
.
. . . .
𝑛
= 1 → 10 = 𝑛 → 𝑘 = log 𝑛 Height = k?
𝑇(1) 𝑇(1) 𝑇(1) 𝑇(1)
10
18
Shortest Path
+ +
Height 𝒏 Cost of level
0 𝑛 𝑛
1 𝑛 𝑛 9𝑛
10 + =𝑛
10 10
2 𝑛 𝑛 9𝑛 9𝑛 81𝑛
100 + + + =𝑛
100 100 100 100
… … …
… … …
𝑘 𝑛 𝑛
=1
10

𝑛
= 1 → 10 = 𝑛 → 𝑘 = log 𝑛
10
19
Longest Path

Height 𝒏 Cost of level


0 𝑛 𝑛 𝑛
Height = 0

1 9𝑛 𝑛 9𝑛
+ =𝑛
10 10 10
𝑛 9𝑛
2 81𝑛 𝑛 9𝑛 9𝑛 81𝑛 Height = 1 10 10
+ + + =𝑛
100 100 100 100 100
… … …
… … … 𝑛 9𝑛 9𝑛 81𝑛
Height = 2 100 100 100 100
𝑘 9 𝑛
( ) 𝑛=1 .
10 . . . .
.
. . . .
9 10 𝑇(1) 𝑇(1) 𝑇(1) 𝑇(1)
( ) 𝑛 = 1 → ( ) = 𝑛 → 𝑘 = log 𝑛 Height = k?
10 9
20
Longest Path
+ +
Height 𝒏 Cost of level
0 𝑛 𝑛
1 9𝑛 𝑛 9𝑛
+ =𝑛
10 10 10
2 81𝑛 𝑛 9𝑛 9𝑛 81𝑛
+ + + =𝑛
100 100 100 100 100
… … …
… … …
𝑘 9 𝑛
( ) 𝑛=1
10

9 10
( ) 𝑛 = 1 → ( ) = 𝑛 → 𝑘 = log 𝑛
10 9

21
Average Case Partitioning

• For counting the average case, assume once we have


worst case partition then best case.

𝑛 Θ(𝑛) 𝑛

𝑛−1 𝑛−1
0 𝑛-1 −1
2 2

𝑛−1 𝑛−1
−1
2 2 In average:
22
Randomized Version of Quicksort

• When calculating the average performance, we assumed that


all possible inputs are equally likely to happen which is not
realistic.
• In practice, the input is many times already sorted (or almost
sorted) leading to Θ(𝑛 ) running time.
• Two ways for randomizing in quicksort:
• Randomly place the input data in the array (time
consuming)
• Randomly choose an element in the input data as a pivot
to partition the data around it. (easier to analyze and
implement)
23
Randomized Version of Quicksort

• Running time is independent of the input


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

24
Randomized Quicksort Algorithm

L x G

x
Average case:

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