SlideShare a Scribd company logo
Quick Sort
 Quick sort is the most efficient internal sorting technique.
 It is also called partitioning sort which uses divide and conquer
techniques.
 The quick sort works by partitioning the array A[1],A[2],……A[n]
by picking some key value in the array as a pivot element.
 Pivot element is used to rearrange the elements in the array.
 Pivot can be the first element of an array and the rest of the
elements are moved so that the elements on left side of the pivot are
lesser than the pivot, whereas those on the right side are greater
than the pivot.
 Now the pivot element is placed in its correct position.
 Then quick sort procedure is applied for left array and right array in
a recursive manner.
Quick Sort Routine
Quick Sort Routine
Example 1
• Consider an unsorted array as follows
40 20 70 14 60 61 97 30
• Here PIVOT=40, i=20, j=30
 The value of i is incremented till a[i]<=Pivot and the value of j is
decremented till a[j]>Pivot, this process is repeated until i<j.
 If a[i]>Pivot and a[j]<Pivot and also if i<j then swap a[i] and a[j]
 If i>j then swap a[j] and a[Pivot]
• Once the correct location for PIVOT is found then partition array
into left sub array and right sub array, where left sub array
contains all the elements less than the PIVOT and right sub array
contains all the elements greater than the PIVOT
Example 1
Passes of Quick Sort
40 20 70 14 60 61 97 30
pivot i j
40 20 70 14 60 61 97 30
pivot i j
Swap(a[i],a[j], i.e As i<j
swap (70,30)
40 20 30 14 60 61 97 70
pivot i j
40 20 30 14 60 61 97 70
pivot i j
40
40
40
40
Example 1
Passes of Quick Sort
40 20 30 14 60 61 97 70
pivot i j
40 20 30 14 60 61 97 70
pivot i j
40 20 30 14 60 61 97 70
pivot i j
40 20 30 14 60 61 97 70
pivot i,j
40
40
40
40
Example 1
Passes of Quick Sort
40 20 30 14 60 61 97 70
pivot j i
As i>j swap (a[j],a[pivot]
i.e swap (60,40)
[[
14 20 30 40 60 61 97 70
• Now the pivot element has reached its correct position.
• The elements lesser than the pivot {14,20,30} is considered as left
sub array.
• The elements greater than the pivot {60,61,97,70} is considered
as right sub array.
• Then qsort procedure is applied recursively for both these arrays.
40
Example 2
Example 2
Example 2
Example 2
Ad

Recommended

Unit 2 - Quick Sort.pptx
Unit 2 - Quick Sort.pptx
CO1IShwetaKidile
 
Divide-and-conquer
Divide-and-conquer
Mrunal Patil
 
Module 2 Quick Sort algorithm in analysis of algorithm.pptx
Module 2 Quick Sort algorithm in analysis of algorithm.pptx
SiddhantRaut20
 
Quick sort
Quick sort
A. S. M. Shafi
 
Quick sort
Quick sort
maamir farooq
 
Quick sort
Quick sort
amar kakde
 
Quick sort
Quick sort
DrHimani Mittal
 
DS - Quick Sort
DS - Quick Sort
MythiliMurugan3
 
CS8391-Data Structures Unit 5
CS8391-Data Structures Unit 5
SIMONTHOMAS S
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
Quicksort algorithm and implantation process
Quicksort algorithm and implantation process
samiulhasan0186
 
Quick sort
Quick sort
Afaq Mansoor Khan
 
quicksort (1).ppt
quicksort (1).ppt
Balasubramanian699229
 
Quick sort by Sania Nisar
Quick sort by Sania Nisar
Sania Nisar
 
quick sort by deepak.pptx
quick sort by deepak.pptx
DeepakM509554
 
Lecture 3.3.4 Quick sort.pptxIIIIIIIIIII
Lecture 3.3.4 Quick sort.pptxIIIIIIIIIII
anshsaini1603
 
MERGE and Quick Sort algorithm explain ppt
MERGE and Quick Sort algorithm explain ppt
UNNATISAXENA8
 
jyothi(22D21A0547)DAA.pptx in DAA computer
jyothi(22D21A0547)DAA.pptx in DAA computer
MadhurimaKomanduru
 
quick_sort_with_explanationandImplmentation.ppt
quick_sort_with_explanationandImplmentation.ppt
MohamedWael807163
 
Data Structure Sorting
Data Structure Sorting
Muhazzab Chouhadry
 
Unit 5 internal sorting &amp; files
Unit 5 internal sorting &amp; files
DrkhanchanaR
 
Quick sort
Quick sort
Dhruv Sabalpara
 
09 QUICK SORT Design and Analysis of algorithms
09 QUICK SORT Design and Analysis of algorithms
syamalamaganti
 
Quick_sort1.pptx
Quick_sort1.pptx
sandeep54552
 
Algorithms - "quicksort"
Algorithms - "quicksort"
Ra'Fat Al-Msie'deen
 
Sorting
Sorting
FahadSaeed39
 
Quick sort.pptx
Quick sort.pptx
Anbarasan Radhakrishnan R
 
Quick sort-Data Structure
Quick sort-Data Structure
Jeanie Arnoco
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 

More Related Content

Similar to Quick Sort algorithm for sorting an array of elements (20)

CS8391-Data Structures Unit 5
CS8391-Data Structures Unit 5
SIMONTHOMAS S
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
Quicksort algorithm and implantation process
Quicksort algorithm and implantation process
samiulhasan0186
 
Quick sort
Quick sort
Afaq Mansoor Khan
 
quicksort (1).ppt
quicksort (1).ppt
Balasubramanian699229
 
Quick sort by Sania Nisar
Quick sort by Sania Nisar
Sania Nisar
 
quick sort by deepak.pptx
quick sort by deepak.pptx
DeepakM509554
 
Lecture 3.3.4 Quick sort.pptxIIIIIIIIIII
Lecture 3.3.4 Quick sort.pptxIIIIIIIIIII
anshsaini1603
 
MERGE and Quick Sort algorithm explain ppt
MERGE and Quick Sort algorithm explain ppt
UNNATISAXENA8
 
jyothi(22D21A0547)DAA.pptx in DAA computer
jyothi(22D21A0547)DAA.pptx in DAA computer
MadhurimaKomanduru
 
quick_sort_with_explanationandImplmentation.ppt
quick_sort_with_explanationandImplmentation.ppt
MohamedWael807163
 
Data Structure Sorting
Data Structure Sorting
Muhazzab Chouhadry
 
Unit 5 internal sorting &amp; files
Unit 5 internal sorting &amp; files
DrkhanchanaR
 
Quick sort
Quick sort
Dhruv Sabalpara
 
09 QUICK SORT Design and Analysis of algorithms
09 QUICK SORT Design and Analysis of algorithms
syamalamaganti
 
Quick_sort1.pptx
Quick_sort1.pptx
sandeep54552
 
Algorithms - "quicksort"
Algorithms - "quicksort"
Ra'Fat Al-Msie'deen
 
Sorting
Sorting
FahadSaeed39
 
Quick sort.pptx
Quick sort.pptx
Anbarasan Radhakrishnan R
 
Quick sort-Data Structure
Quick sort-Data Structure
Jeanie Arnoco
 
CS8391-Data Structures Unit 5
CS8391-Data Structures Unit 5
SIMONTHOMAS S
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
Quicksort algorithm and implantation process
Quicksort algorithm and implantation process
samiulhasan0186
 
Quick sort by Sania Nisar
Quick sort by Sania Nisar
Sania Nisar
 
quick sort by deepak.pptx
quick sort by deepak.pptx
DeepakM509554
 
Lecture 3.3.4 Quick sort.pptxIIIIIIIIIII
Lecture 3.3.4 Quick sort.pptxIIIIIIIIIII
anshsaini1603
 
MERGE and Quick Sort algorithm explain ppt
MERGE and Quick Sort algorithm explain ppt
UNNATISAXENA8
 
jyothi(22D21A0547)DAA.pptx in DAA computer
jyothi(22D21A0547)DAA.pptx in DAA computer
MadhurimaKomanduru
 
quick_sort_with_explanationandImplmentation.ppt
quick_sort_with_explanationandImplmentation.ppt
MohamedWael807163
 
Unit 5 internal sorting &amp; files
Unit 5 internal sorting &amp; files
DrkhanchanaR
 
09 QUICK SORT Design and Analysis of algorithms
09 QUICK SORT Design and Analysis of algorithms
syamalamaganti
 
Quick sort-Data Structure
Quick sort-Data Structure
Jeanie Arnoco
 

Recently uploaded (20)

Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
Engineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its Application
Sakthivel M
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
Structured Programming with C++ :: Kjell Backman
Structured Programming with C++ :: Kjell Backman
Shabista Imam
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
23Q95A6706
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
ijab2
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
ravicivil
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
Engineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its Application
Sakthivel M
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
Structured Programming with C++ :: Kjell Backman
Structured Programming with C++ :: Kjell Backman
Shabista Imam
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
23Q95A6706
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
ijab2
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
ravicivil
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
Ad

Quick Sort algorithm for sorting an array of elements

  • 1. Quick Sort  Quick sort is the most efficient internal sorting technique.  It is also called partitioning sort which uses divide and conquer techniques.  The quick sort works by partitioning the array A[1],A[2],……A[n] by picking some key value in the array as a pivot element.  Pivot element is used to rearrange the elements in the array.  Pivot can be the first element of an array and the rest of the elements are moved so that the elements on left side of the pivot are lesser than the pivot, whereas those on the right side are greater than the pivot.  Now the pivot element is placed in its correct position.  Then quick sort procedure is applied for left array and right array in a recursive manner.
  • 4. Example 1 • Consider an unsorted array as follows 40 20 70 14 60 61 97 30 • Here PIVOT=40, i=20, j=30  The value of i is incremented till a[i]<=Pivot and the value of j is decremented till a[j]>Pivot, this process is repeated until i<j.  If a[i]>Pivot and a[j]<Pivot and also if i<j then swap a[i] and a[j]  If i>j then swap a[j] and a[Pivot] • Once the correct location for PIVOT is found then partition array into left sub array and right sub array, where left sub array contains all the elements less than the PIVOT and right sub array contains all the elements greater than the PIVOT
  • 5. Example 1 Passes of Quick Sort 40 20 70 14 60 61 97 30 pivot i j 40 20 70 14 60 61 97 30 pivot i j Swap(a[i],a[j], i.e As i<j swap (70,30) 40 20 30 14 60 61 97 70 pivot i j 40 20 30 14 60 61 97 70 pivot i j 40 40 40 40
  • 6. Example 1 Passes of Quick Sort 40 20 30 14 60 61 97 70 pivot i j 40 20 30 14 60 61 97 70 pivot i j 40 20 30 14 60 61 97 70 pivot i j 40 20 30 14 60 61 97 70 pivot i,j 40 40 40 40
  • 7. Example 1 Passes of Quick Sort 40 20 30 14 60 61 97 70 pivot j i As i>j swap (a[j],a[pivot] i.e swap (60,40) [[ 14 20 30 40 60 61 97 70 • Now the pivot element has reached its correct position. • The elements lesser than the pivot {14,20,30} is considered as left sub array. • The elements greater than the pivot {60,61,97,70} is considered as right sub array. • Then qsort procedure is applied recursively for both these arrays. 40