ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
By: 3Algo
Q
U
I
C T
S
O
RK
Quicksort or partition-exchange sort is an efficient sorting algorithm developed by Tony
Hoare in 1959 while in the Soviet Union, as a visiting student at Moscow State University.
At that time, he worked on a project on machine translation for the National Physical
Laboratory. (Wikipedia)
Quicksort Variants
1. Multi-pivot Quicksort : Instead of partition array into two subarrays, it partitions into some s number of subarrays using s
¨C 1 pivot. In mid-1970s, the dual-pivot case was considered by Sedgewick and others, but the resulting algorithm is not faster
than the ¡°classical¡± quicksort in practice. However, it can be more efficient by making uses of processor caches. It is more
related to cache performance.
2. External Quicksort : Everything is the same as a regular quick sort except the pivot is replaced by a buffer. It is a kind of
three-way quicksort in which the middle partition (buffer) represents a sorted subarray of elements that are approximately
equal to the pivot.
3. Three-way radix Quicksort : A combination of radix sort and quicksort.
4. Quick radix sort : A combination of radix sort and quicksort but left/right partition decision is made on successive bits of
the key.
5. BlockQuicksort : Rearranges the computations of quicksort to convert unpredictable branches to data dependencies.
6. Partial and incremental Quicksort : Several variants of quicksort that separate the k smallest or largest elements from
the rest of the input.
Partition Scheme
The choice of pivot and partitioning steps could
greatly affects the algorithm¡¯s performance.
Below are the two scheme:
Lomuto Partition Scheme pseudocode
algorithm paritition(A, lo, hi) is
pivot := A[hi]
i := lo
for j := lo to hi do
if A[j] < pivot then
swap A[i] with A[j]
i := I + 1
swap A[i] with A[hi]
return i
Hoare Partition Scheme
algorithm partition(A, lo, hi) is
pivot := A[(hi + lo) / 2]
i := lo ¨C 1
j := hi + 1
loop forever
do
i := i + 1
while A[i] < pivot
do
j := j - 1
while A[j] > pivot
if i >= j then
return j
swap A[i] with A[j]
Algorithm:
algorithm quicksort(A, lo, hi) is
if lo < hi then
p := partition(A, lo, hi)
quicksort(A, lo, p)
quicksort(A, p + 1, hi)
Example: Quicksort using Hoare Partition Scheme
Value 12 5 29 0 22 5 7
Index 0 1 2 3 4 5 6
Let sort this array from index 1 to index 5
Value 5 29 0 22 5
Index 0 1 2 3 4
lo = 0, hi = 4, pivot = 0, swap(A[0], A[2])
Value 0 29 5 22 5
Index 0 1 2 3 4
Recursively call:
+ quicksort(A, 0, 0)
+ quicksort(A, 1, 4)
Value 0 5 5 22 29
Index 0 1 2 3 4
lo = 1, hi = 4, pivot = 5, swap(A[1], A[4])
Recursively call:
+ quicksort(A, 1, 1)
+ quicksort(A, 2, 2)
+ quicksort(A, 3, 4)
lo = 1, hi = 2, pivot = 5, swap(A[1], A[2])
Recursively call:
+ quicksort(A, 1, 2)
lo = 3, hi = 4, pivot = 22
Recursively call:
+ quicksort(A, 3, 3)
+ quicksort(A, 4, 4)
Value 0 5 5 22 29
Index 0 1 2 3 4
Result:
Resources
1. https://dl.acm.org/doi/pdf/10.1145/366622.366644
2. https://en.wikipedia.org/wiki/Quicksort

More Related Content

Similar to Quicksort (20)

PPTX
Algorithms - "quicksort"
Ra'Fat Al-Msie'deen
?
PPT
3.8 quick sort
Krish_ver2
?
PPTX
Quick-Sort Algorithm and pivot selection
SubhranjaliBehera
?
PPTX
Quick sort
Afaq Mansoor Khan
?
PPTX
Quicksort Presentation
irdginfo
?
PPTX
jyothi(22D21A0547)DAA.pptx in DAA computer
MadhurimaKomanduru
?
PPT
Quick sort Algorithm Discussion And Analysis
SNJ Chaudhary
?
PPTX
Complexity of algorithms
Jasur Ahmadov
?
PDF
A Survey of Adaptive QuickSort Algorithms
CSCJournals
?
PDF
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
?
PPTX
Quick_Sort.pptx
NishantShah863796
?
PPT
Quick Sort
Soumen Santra
?
PDF
Quicksort
Vasileios Lampos
?
PDF
Quick Sort By Prof Lili Saghafi
Professor Lili Saghafi
?
PDF
Heap, quick and merge sort
Dr. Mohammad Amir Khusru Akhtar (Ph.D)
?
PPTX
Sorting
FahadSaeed39
?
PPS
Quick sort
DrHimani Mittal
?
PPTX
Quick sort by Sania Nisar
Sania Nisar
?
PPT
3.8 quicksort
Krish_ver2
?
Algorithms - "quicksort"
Ra'Fat Al-Msie'deen
?
3.8 quick sort
Krish_ver2
?
Quick-Sort Algorithm and pivot selection
SubhranjaliBehera
?
Quicksort Presentation
irdginfo
?
jyothi(22D21A0547)DAA.pptx in DAA computer
MadhurimaKomanduru
?
Quick sort Algorithm Discussion And Analysis
SNJ Chaudhary
?
Complexity of algorithms
Jasur Ahmadov
?
A Survey of Adaptive QuickSort Algorithms
CSCJournals
?
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
?
Quick_Sort.pptx
NishantShah863796
?
Quick Sort
Soumen Santra
?
Quick Sort By Prof Lili Saghafi
Professor Lili Saghafi
?
Heap, quick and merge sort
Dr. Mohammad Amir Khusru Akhtar (Ph.D)
?
Sorting
FahadSaeed39
?
Quick sort
DrHimani Mittal
?
Quick sort by Sania Nisar
Sania Nisar
?
3.8 quicksort
Krish_ver2
?

Recently uploaded (20)

PPTX
How to use _name_search() method in Odoo 18
Celine George
?
PPTX
SYMPATHOMIMETICS[ADRENERGIC AGONISTS] pptx
saip95568
?
PDF
Rapid Mathematics Assessment Score sheet for all Grade levels
DessaCletSantos
?
PPTX
JSON, XML and Data Science introduction.pptx
Ramakrishna Reddy Bijjam
?
PDF
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
?
DOCX
MUSIC AND ARTS 5 DLL MATATAG LESSON EXEMPLAR QUARTER 1_Q1_W1.docx
DianaValiente5
?
PDF
VCE Literature Section A Exam Response Guide
jpinnuck
?
PPTX
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
?
PPTX
Project 4 PART 1 AI Assistant Vocational Education
barmanjit380
?
PDF
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
?
PDF
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
?
PPTX
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
?
PPTX
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
?
PDF
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
?
PPTX
ESP 10 Edukasyon sa Pagpapakatao PowerPoint Lessons Quarter 1.pptx
Sir J.
?
PDF
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
?
PDF
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde¡¯s The Nightingal...
KaryanaTantri21
?
PDF
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
?
PPTX
Elo the HeroTHIS IS A STORY ABOUT A BOY WHO SAVED A LITTLE GOAT .pptx
JoyIPanos
?
PDF
COM and NET Component Services 1st Edition Juval L?wy
kboqcyuw976
?
How to use _name_search() method in Odoo 18
Celine George
?
SYMPATHOMIMETICS[ADRENERGIC AGONISTS] pptx
saip95568
?
Rapid Mathematics Assessment Score sheet for all Grade levels
DessaCletSantos
?
JSON, XML and Data Science introduction.pptx
Ramakrishna Reddy Bijjam
?
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
?
MUSIC AND ARTS 5 DLL MATATAG LESSON EXEMPLAR QUARTER 1_Q1_W1.docx
DianaValiente5
?
VCE Literature Section A Exam Response Guide
jpinnuck
?
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
?
Project 4 PART 1 AI Assistant Vocational Education
barmanjit380
?
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
?
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
?
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
?
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
?
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
?
ESP 10 Edukasyon sa Pagpapakatao PowerPoint Lessons Quarter 1.pptx
Sir J.
?
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
?
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde¡¯s The Nightingal...
KaryanaTantri21
?
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
?
Elo the HeroTHIS IS A STORY ABOUT A BOY WHO SAVED A LITTLE GOAT .pptx
JoyIPanos
?
COM and NET Component Services 1st Edition Juval L?wy
kboqcyuw976
?
Ad

Quicksort

  • 2. Quicksort or partition-exchange sort is an efficient sorting algorithm developed by Tony Hoare in 1959 while in the Soviet Union, as a visiting student at Moscow State University. At that time, he worked on a project on machine translation for the National Physical Laboratory. (Wikipedia)
  • 3. Quicksort Variants 1. Multi-pivot Quicksort : Instead of partition array into two subarrays, it partitions into some s number of subarrays using s ¨C 1 pivot. In mid-1970s, the dual-pivot case was considered by Sedgewick and others, but the resulting algorithm is not faster than the ¡°classical¡± quicksort in practice. However, it can be more efficient by making uses of processor caches. It is more related to cache performance. 2. External Quicksort : Everything is the same as a regular quick sort except the pivot is replaced by a buffer. It is a kind of three-way quicksort in which the middle partition (buffer) represents a sorted subarray of elements that are approximately equal to the pivot. 3. Three-way radix Quicksort : A combination of radix sort and quicksort. 4. Quick radix sort : A combination of radix sort and quicksort but left/right partition decision is made on successive bits of the key. 5. BlockQuicksort : Rearranges the computations of quicksort to convert unpredictable branches to data dependencies. 6. Partial and incremental Quicksort : Several variants of quicksort that separate the k smallest or largest elements from the rest of the input.
  • 4. Partition Scheme The choice of pivot and partitioning steps could greatly affects the algorithm¡¯s performance. Below are the two scheme: Lomuto Partition Scheme pseudocode algorithm paritition(A, lo, hi) is pivot := A[hi] i := lo for j := lo to hi do if A[j] < pivot then swap A[i] with A[j] i := I + 1 swap A[i] with A[hi] return i Hoare Partition Scheme algorithm partition(A, lo, hi) is pivot := A[(hi + lo) / 2] i := lo ¨C 1 j := hi + 1 loop forever do i := i + 1 while A[i] < pivot do j := j - 1 while A[j] > pivot if i >= j then return j swap A[i] with A[j]
  • 5. Algorithm: algorithm quicksort(A, lo, hi) is if lo < hi then p := partition(A, lo, hi) quicksort(A, lo, p) quicksort(A, p + 1, hi) Example: Quicksort using Hoare Partition Scheme Value 12 5 29 0 22 5 7 Index 0 1 2 3 4 5 6 Let sort this array from index 1 to index 5 Value 5 29 0 22 5 Index 0 1 2 3 4 lo = 0, hi = 4, pivot = 0, swap(A[0], A[2]) Value 0 29 5 22 5 Index 0 1 2 3 4 Recursively call: + quicksort(A, 0, 0) + quicksort(A, 1, 4) Value 0 5 5 22 29 Index 0 1 2 3 4 lo = 1, hi = 4, pivot = 5, swap(A[1], A[4]) Recursively call: + quicksort(A, 1, 1) + quicksort(A, 2, 2) + quicksort(A, 3, 4) lo = 1, hi = 2, pivot = 5, swap(A[1], A[2]) Recursively call: + quicksort(A, 1, 2) lo = 3, hi = 4, pivot = 22 Recursively call: + quicksort(A, 3, 3) + quicksort(A, 4, 4) Value 0 5 5 22 29 Index 0 1 2 3 4 Result: