SlideShare a Scribd company logo
Quick Sort
Dr. Himani Mittal
Quick Sort
Quicksort is a divide-and-conquer sorting
algorithm in which division is dynamically
carried out (as opposed to static division in
Mergesort).
The three steps of Quicksort are as follows:
• Divide: Rearrange the elements and split the array into two subarrays
and an element in between such that so that each element in the left
subarray is less than or equal the middle element and each element
in the right subarray is greater than the middle element.
• Conquer: Recursively sort the two subarrays.
• Combine: None.
Quick Sort
15 20 5 30 35 10 ∞
20 < 15 false so stop
∞<= 1510 > 15 false
so stop
i =2
m =1
j =7
i< j, interchange a[i] with a[j]
15 10 5 30 35 20 ∞
i < j (3 <6), true so continue in loop with i=2 and j=6
15 10 5 30 35 20 ∞
5 <15
true
30 <15 false so stop for I = 4
35 >15 true
30>15
i =3
m =1
j =5
i< j, false, so no interchange.
Repeat condition becomes true, So stop looping. interchange a[m] with a[j]
5>15 false so
stop for j=3
5 10 15 30 35 20 ∞
Return j=3 as new pivot element
30 35 20 ∞
35>=30
20 <=30i =4
m =4
j =7
i< j, interchange a[i] with a[j] => 5< 6 , so change a[5] with a[6]
30 20 35 ∞
i < j, (5 < 6) true so continue in loop With i=5 and j=6
30 20 35 ∞
35 >3020 <30i =6
m =4
j =5
i< j, false,No interchange.
While(i<j) becomes false , So stop looping .interchange a[m] with a[j]
Return j=5 as new pivot element
Two partitions: partition(a,1,2) and partition(a,4,7)
20 30 35 ∞
Quick Sort Call Tree
p = 1, q = 6
j = 3
p = 4, q = 6
j = 5
p = 1, q = 2
j = 1
p = 2, q = 2
return
p = 1, q = 0
return
p = 6, q = 6
return
p = 4, q = 4
return
Though this tree seems balanced, in Quick Sort the tree is not always balanced
because the partition position j will change according to its position in sorting
order. This position will change according to input array.
So best case is that array always splits in middle.
Worst case is left or right skewed.
Average case is array can split at any position s.

More Related Content

What's hot

TechMathII - 1.6 - Matrices
TechMathII - 1.6 - MatricesTechMathII - 1.6 - Matrices
TechMathII - 1.6 - Matrices
lmrhodes
 
Graphing linear inequalities
Graphing linear inequalitiesGraphing linear inequalities
Graphing linear inequalities
sphbbard
 
Integers
IntegersIntegers
Integers
Punita Verma
 
Operations Of Integers
Operations Of IntegersOperations Of Integers
Operations Of Integers
swright210
 
Roots and radical expressions
Roots and radical expressionsRoots and radical expressions
Roots and radical expressions
holmsted
 
Ncert solutions for class 7 maths chapter 1 integers exercise 1
Ncert solutions for class 7 maths chapter 1 integers exercise 1Ncert solutions for class 7 maths chapter 1 integers exercise 1
Ncert solutions for class 7 maths chapter 1 integers exercise 1
iprepkumar
 
Operation research - Chapter 03
Operation research - Chapter 03Operation research - Chapter 03
Operation research - Chapter 03
2013901097
 
Chapter1, Sec 9 Multiplying Dividing Integers
Chapter1, Sec 9 Multiplying Dividing IntegersChapter1, Sec 9 Multiplying Dividing Integers
Chapter1, Sec 9 Multiplying Dividing Integers
Jessca Lundin
 
Business Math Chapter 1
Business Math Chapter 1Business Math Chapter 1
Business Math Chapter 1
Nazrin Nazdri
 
Integers best ppt
Integers best pptIntegers best ppt
Integers best ppt
arivuselvi3
 
add and subtract similar fractions
add and subtract similar fractionsadd and subtract similar fractions
add and subtract similar fractions
JohnTitoLerios
 
Integers
IntegersIntegers
Associative Property
Associative PropertyAssociative Property
Associative Property
hiratufail
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sort
Krish_ver2
 
AI assignment fuzzylogic
AI assignment fuzzylogicAI assignment fuzzylogic
AI assignment fuzzylogic
AuliaPrameswari2
 
Simplifying basic radical expressions
Simplifying basic radical expressionsSimplifying basic radical expressions
Simplifying basic radical expressions
DaisyListening
 
business mathematics
business mathematicsbusiness mathematics
business mathematics
nicerence
 
Matrices & Determinants
Matrices & DeterminantsMatrices & Determinants
Matrices & Determinants
Ishant Jain
 
Operations research - Chapter 04
Operations research - Chapter 04Operations research - Chapter 04
Operations research - Chapter 04
2013901097
 
Operation research - Chapter 02
Operation research - Chapter 02Operation research - Chapter 02
Operation research - Chapter 02
2013901097
 

What's hot (20)

TechMathII - 1.6 - Matrices
TechMathII - 1.6 - MatricesTechMathII - 1.6 - Matrices
TechMathII - 1.6 - Matrices
 
Graphing linear inequalities
Graphing linear inequalitiesGraphing linear inequalities
Graphing linear inequalities
 
Integers
IntegersIntegers
Integers
 
Operations Of Integers
Operations Of IntegersOperations Of Integers
Operations Of Integers
 
Roots and radical expressions
Roots and radical expressionsRoots and radical expressions
Roots and radical expressions
 
Ncert solutions for class 7 maths chapter 1 integers exercise 1
Ncert solutions for class 7 maths chapter 1 integers exercise 1Ncert solutions for class 7 maths chapter 1 integers exercise 1
Ncert solutions for class 7 maths chapter 1 integers exercise 1
 
Operation research - Chapter 03
Operation research - Chapter 03Operation research - Chapter 03
Operation research - Chapter 03
 
Chapter1, Sec 9 Multiplying Dividing Integers
Chapter1, Sec 9 Multiplying Dividing IntegersChapter1, Sec 9 Multiplying Dividing Integers
Chapter1, Sec 9 Multiplying Dividing Integers
 
Business Math Chapter 1
Business Math Chapter 1Business Math Chapter 1
Business Math Chapter 1
 
Integers best ppt
Integers best pptIntegers best ppt
Integers best ppt
 
add and subtract similar fractions
add and subtract similar fractionsadd and subtract similar fractions
add and subtract similar fractions
 
Integers
IntegersIntegers
Integers
 
Associative Property
Associative PropertyAssociative Property
Associative Property
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sort
 
AI assignment fuzzylogic
AI assignment fuzzylogicAI assignment fuzzylogic
AI assignment fuzzylogic
 
Simplifying basic radical expressions
Simplifying basic radical expressionsSimplifying basic radical expressions
Simplifying basic radical expressions
 
business mathematics
business mathematicsbusiness mathematics
business mathematics
 
Matrices & Determinants
Matrices & DeterminantsMatrices & Determinants
Matrices & Determinants
 
Operations research - Chapter 04
Operations research - Chapter 04Operations research - Chapter 04
Operations research - Chapter 04
 
Operation research - Chapter 02
Operation research - Chapter 02Operation research - Chapter 02
Operation research - Chapter 02
 

Recently uploaded

Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdfIGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
Amin Marwan
 
ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
dot55audits
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Leena Ghag-Sakpal
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 

Recently uploaded (20)

Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdfIGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
 
ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 

Quick sort

  • 2. Quick Sort Quicksort is a divide-and-conquer sorting algorithm in which division is dynamically carried out (as opposed to static division in Mergesort).
  • 3. The three steps of Quicksort are as follows: • Divide: Rearrange the elements and split the array into two subarrays and an element in between such that so that each element in the left subarray is less than or equal the middle element and each element in the right subarray is greater than the middle element. • Conquer: Recursively sort the two subarrays. • Combine: None. Quick Sort
  • 4.
  • 5.
  • 6. 15 20 5 30 35 10 ∞ 20 < 15 false so stop ∞<= 1510 > 15 false so stop i =2 m =1 j =7 i< j, interchange a[i] with a[j] 15 10 5 30 35 20 ∞ i < j (3 <6), true so continue in loop with i=2 and j=6 15 10 5 30 35 20 ∞ 5 <15 true 30 <15 false so stop for I = 4 35 >15 true 30>15 i =3 m =1 j =5 i< j, false, so no interchange. Repeat condition becomes true, So stop looping. interchange a[m] with a[j] 5>15 false so stop for j=3 5 10 15 30 35 20 ∞ Return j=3 as new pivot element
  • 7. 30 35 20 ∞ 35>=30 20 <=30i =4 m =4 j =7 i< j, interchange a[i] with a[j] => 5< 6 , so change a[5] with a[6] 30 20 35 ∞ i < j, (5 < 6) true so continue in loop With i=5 and j=6 30 20 35 ∞ 35 >3020 <30i =6 m =4 j =5 i< j, false,No interchange. While(i<j) becomes false , So stop looping .interchange a[m] with a[j] Return j=5 as new pivot element Two partitions: partition(a,1,2) and partition(a,4,7) 20 30 35 ∞
  • 8. Quick Sort Call Tree p = 1, q = 6 j = 3 p = 4, q = 6 j = 5 p = 1, q = 2 j = 1 p = 2, q = 2 return p = 1, q = 0 return p = 6, q = 6 return p = 4, q = 4 return Though this tree seems balanced, in Quick Sort the tree is not always balanced because the partition position j will change according to its position in sorting order. This position will change according to input array. So best case is that array always splits in middle. Worst case is left or right skewed. Average case is array can split at any position s.