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

Quick Sort-Protected-1 - Unlocked

Uploaded by

kausha0809
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)
8 views

Quick Sort-Protected-1 - Unlocked

Uploaded by

kausha0809
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/ 4

12-09-2023

Quicksort
Bachelor of Technology
CSE (AIML)/ICT
• Follows the divide-and-conquer paradigm.

• Divide: Partition (divide) the array A[p..r] into two (possibly empty) sub-arrays A[p..q–1] and
Analysis and Design of Algorithms A[q+1..r] such that:
Each element in A[p..q–1] ≤ A[q] ≤ each element in A[q+1..r]
Module 1 - Introduction
Quicksort Index q is computed as part of the partitioning procedure.
• Conquer: Sort the two sub-arrays by recursive calls to quicksort.

• Combine: The sub-arrays are sorted in place – no work is needed to combine them.
Dr. Anuj Kr. Singh
Associate Professor • How the divide and combine steps of quicksort can be compared with those of merge sort?
Computer Science & Engineering

12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 2

Quicksort Quick Sort

Example 2 8 7 1 3 6 4 PARTITION(A, p, r)
QUICKSORT(A, p, r) PARTITION(A, p, r) 1 x = A[r]
1 if p < r then 1 x = A[r] // Pivot Element 2 i=p–1
2 i=p–1 3 for j = p to r – 1 do
2 q = PARTITION(A, p, r) // Divide
4 if A[j]  x then
3 QUICKSORT(A, p, q – 1) // Conquer 3 for j = p to r – 1 do
5 i=i+1
4 QUICKSORT(A, q + 1, r) // Conquer 4 if A[j]  x then 6 A[i]  A[j] //Swapping
5 i=i+1 7 A[i + 1]  A[r] //Swapping
6 A[i]  A[j] //Swapping 8 return i + 1

7 A[i + 1]  A[r] //Swapping


Example
8 return i + 1
2 8 7 1 3 6 4
Running Time of Partitioning?

12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 3 12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 4
12-09-2023

Quick Sort Performance of Quicksort

Example 2 8 7 1 3 6 4 • Will depend upon how many times the


PARTITIONING is called.
1

• Best case partitioning 2


• Worst case partitioning
Best Case 3
• Average case partitioning

Worst Case

Average Case

12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 5 12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 6

Performance of Quicksort Performance of Quicksort

Best Case When the partitioning element is placed at the middle of the array Worst Case When the partitioning element is placed at any one corner of the
Partitioning 01 every time. 02 array every time.
Partitioning

T(n) = 2 T(n/2) + n T(n) = T(n−1) + n

12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 7 12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 8
12-09-2023

Performance of Quicksort Performance of Quicksort

Average Case When the partitioning element is placed neither at the middle not
Partitioning 03 at any corner of the array every time.
T(n) = 2 T(n/2) + n - Best case

T(n) = T(𝛼 n) + T((1 − 𝛼 )n) + n T(n) = T(n-1) + n – Worst case

T(n) = T(𝛼 n) + T((1 − 𝛼 )n) + n – Average case

12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 9 12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 10

Randomized Quicksort Fibonacci Search

RANDOMIZED-QUICKSORT(A, p, r) Algorithm F(n) • Fibonacci Search


1 if p < r then
if n == 0 or n == 1 then
2 q = RANDOMIZED-PARTITION(A, p, r) // Divide Fibonacci Search is a comparison-based technique that uses
3 RANDOMIZED-QUICKSORT(A, p, q – 1) // Conquer return 1 Fibonacci numbers to search an element in a sorted array.
4 RANDOMIZED-QUICKSORT(A, q + 1, r) // Conquer else

RANDOMIZED-PARTITION(A, p, r)
return F(n-1)+F(n-2) • Works for sorted arrays
1 i = RANDOM [p, r] • A Divide and Conquer Algorithm.
2 A[i ]  A[r] • Has running time of order lg n.
3 return PARTITION (A, p, r)

Example

2 8 7 1 3 6 4

12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 11 12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 12
12-09-2023

Medians and Order Statistics Maximum and Minimum

• A median, informally, is the “halfway point” of


ith smallest
the set.
element
Median
ith order statistic
of a set of n
elements
When n is odd When n is even

Two Medians
• The minimum of a set of elements is the Unique Median 𝑖 = 𝑛/2
first order statistic (i = 1),and the 𝑖 = (𝑛 + 1)/2 𝑛
𝑖 = +1 • Simultaneous minimum and maximum
maximum is the nth order statistic (i = n). 2

12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 13 12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 14

ith Order Statistics

12-09-2023 Dr. Anuj Kr. Singh, Associate Professor 15

You might also like