SlideShare a Scribd company logo
1 of 8
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 - Matriceslmrhodes
 
Graphing linear inequalities
Graphing linear inequalitiesGraphing linear inequalities
Graphing linear inequalitiessphbbard
 
Operations Of Integers
Operations Of IntegersOperations Of Integers
Operations Of Integersswright210
 
Roots and radical expressions
Roots and radical expressionsRoots and radical expressions
Roots and radical expressionsholmsted
 
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 1iprepkumar
 
Operation research - Chapter 03
Operation research - Chapter 03Operation research - Chapter 03
Operation research - Chapter 032013901097
 
Chapter1, Sec 9 Multiplying Dividing Integers
Chapter1, Sec 9 Multiplying Dividing IntegersChapter1, Sec 9 Multiplying Dividing Integers
Chapter1, Sec 9 Multiplying Dividing IntegersJessca Lundin
 
Business Math Chapter 1
Business Math Chapter 1Business Math Chapter 1
Business Math Chapter 1Nazrin Nazdri
 
Integers best ppt
Integers best pptIntegers best ppt
Integers best pptarivuselvi3
 
add and subtract similar fractions
add and subtract similar fractionsadd and subtract similar fractions
add and subtract similar fractionsJohnTitoLerios
 
Associative Property
Associative PropertyAssociative Property
Associative Propertyhiratufail
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sortKrish_ver2
 
AI assignment fuzzylogic
AI assignment fuzzylogicAI assignment fuzzylogic
AI assignment fuzzylogicAuliaPrameswari2
 
Simplifying basic radical expressions
Simplifying basic radical expressionsSimplifying basic radical expressions
Simplifying basic radical expressionsDaisyListening
 
business mathematics
business mathematicsbusiness mathematics
business mathematicsnicerence
 
Matrices & Determinants
Matrices & DeterminantsMatrices & Determinants
Matrices & DeterminantsIshant Jain
 
Operations research - Chapter 04
Operations research - Chapter 04Operations research - Chapter 04
Operations research - Chapter 042013901097
 
Operation research - Chapter 02
Operation research - Chapter 02Operation research - Chapter 02
Operation research - Chapter 022013901097
 

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

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 

Recently uploaded (20)

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 

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.