SlideShare a Scribd company logo
Quick Sort
Quicksort is a sorting algorithm based on the divide and conquer approach where
 An array is divided into subarrays by selecting a pivot element (element selected from the
array). While dividing the array, the pivot element should be positioned in such a way that
elements less than pivot are kept on the left side and elements greater than pivot are on the
right side of the pivot.
 The left and right subarrays are also divided using the same approach. This process
continues until each subarray contains a single element.
 At this point, elements are already sorted. Finally, elements are combined to form a sorted
array.
Example:
0 1 2 3 4 5 6 7
50=i 30 10 90 80 20 40 70=j
Pivot=50
0 1 2 3 4 5 6 7
50 30 10 90=i 80 20 40=j 70
0 1 2 3 4 5 6 7
50 30 10 40=i 80 20 90=j 70
0 1 2 3 4 5 6 7
50 30 10 40 80=i 20=j 90 70
0 1 2 3 4 5 6 7
50 30 10 40 20=i 80=j 90 70
0 1 2 3 4 5 6 7
50 30 10 40 20=j 80=i 90 70
0 1 2 3 4 5 6 7
20 30 10 40 50 80 90 70
Left Half
0 1 2 3
20=i 30 10 40=j
Pivot=20
0 1 2 3
20 30=i 10=j 40
0 1 2 3
20 10=i 30=j 40
0 1 2 3
20 10=j 30=i 40
0 1 2 3
10 20 30 40
Right Half
5 6 7
80=i 90 70=j
Pivot=80
5 6 7
80 90=i 70=j
5 6 7
80 70=i 90=j
5 6 7
80 70=j 90=i
5 6 7
70 80 90
Finally
0 1 2 3 4 5 6 7
10 20 30 40 50 70 80 90
Algorithm QuickSort(l,h)
{
if(l<h)
{
j=Partition(l,h);
QuickSort(l,j);
QuickSort(j+1,h);
}
}
Algorithm Partition(l,h)
{
Pivot=A[l];
i=l,j=h;
while(i<j)
{
do
{
i++;
}while(A[i]<=pivot);
do
{
j--;
}while(A[j]>pivot);
if(i<j)
swap(A[i],A[j]);
}
Swap(A[l],A[j]);
return j;
}

More Related Content

PPTX
Bucket sort
PPTX
Insertion sort algorithm power point presentation
PPTX
Insertion sort
PPT
Counting sort(Non Comparison Sort)
PPT
3.2 insertion sort
PPTX
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PDF
Dijkstra algorithm
Bucket sort
Insertion sort algorithm power point presentation
Insertion sort
Counting sort(Non Comparison Sort)
3.2 insertion sort
Data Structures - Lecture 8 [Sorting Algorithms]
Dijkstra algorithm

What's hot (20)

PPT
Algorithm: Quick-Sort
PPTX
Insertion sort
PDF
double linked list
PPTX
Quick sort
PPTX
Insertion sort and shell sort
PPT
Shell sort
PDF
Binary search algorithm
PPTX
Insertion Sorting
PPT
Insertion sort
PPTX
Presentation on the topic selection sort
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
PPTX
Divide and conquer - Quick sort
PPT
Selection sort
PPTX
Quick sort
PPTX
SORTING techniques.pptx
PPT
Stack Operation In Data Structure
PPTX
Bucket sort
PDF
Quick sort
PPT
Knapsack problem
Algorithm: Quick-Sort
Insertion sort
double linked list
Quick sort
Insertion sort and shell sort
Shell sort
Binary search algorithm
Insertion Sorting
Insertion sort
Presentation on the topic selection sort
Trees, Binary Search Tree, AVL Tree in Data Structures
Divide and conquer - Quick sort
Selection sort
Quick sort
SORTING techniques.pptx
Stack Operation In Data Structure
Bucket sort
Quick sort
Knapsack problem
Ad

Similar to Quick sort (20)

PPTX
Module 2 Quick Sort algorithm in analysis of algorithm.pptx
PPTX
Divide-and-conquer
PPTX
Unit 2 - Quick Sort.pptx
PPTX
Quick Sort algorithm for sorting an array of elements
PPTX
MERGE and Quick Sort algorithm explain ppt
PPTX
Unit 5 internal sorting &amp; files
PPT
3.8 quicksort 04
DOCX
Quick sort
PPTX
09 QUICK SORT Design and Analysis of algorithms
PPT
quick_sort.ppt
PPTX
Quicksort algorithm and implantation process
PPTX
Quick sort
PPT
quicksort.ppthhhhhhhhhhhhhhhhhhhhhhhhhhh
PPT
Quicksort and MergeSort Algorithm Analysis
PPS
Quick sort
PPT
quicksort.ppt
PPT
quicksort (1).ppt
PPT
Quick & Merge Sort.ppt
PPT
quicksort.ppt
PPT
quicksort.ppt
Module 2 Quick Sort algorithm in analysis of algorithm.pptx
Divide-and-conquer
Unit 2 - Quick Sort.pptx
Quick Sort algorithm for sorting an array of elements
MERGE and Quick Sort algorithm explain ppt
Unit 5 internal sorting &amp; files
3.8 quicksort 04
Quick sort
09 QUICK SORT Design and Analysis of algorithms
quick_sort.ppt
Quicksort algorithm and implantation process
Quick sort
quicksort.ppthhhhhhhhhhhhhhhhhhhhhhhhhhh
Quicksort and MergeSort Algorithm Analysis
Quick sort
quicksort.ppt
quicksort (1).ppt
Quick & Merge Sort.ppt
quicksort.ppt
quicksort.ppt
Ad

More from A. S. M. Shafi (20)

DOCX
Data Warehouse Schema (Star, Snowflake).docx
PDF
Correlation Analysis in Machine Learning.pdf
PDF
Naive Bayes and Decision Tree Algorithm.pdf
PDF
Frequent Pattern Growth Mining Algorithm.pdf
PDF
Direct Hashing and Pruning Algorithm in Data MIning.pdf
PDF
Association Rule Mining with Apriori Algorithm.pdf
PDF
HITS Algorithm in Data and Web MIning.pdf
PDF
Page Rank Algorithm in Data Mining and Web Application.pdf
PDF
K Nearest Neighbor Classifier in Machine Learning.pdf
PDF
K Means Clustering Algorithm in Machine Learning.pdf
PDF
2D Transformation in Computer Graphics
PDF
3D Transformation in Computer Graphics
PDF
Projection
PDF
2D Transformation
PDF
Line drawing algorithm
PDF
Fragmentation
PDF
File organization
PDF
Bankers algorithm
PDF
RR and priority scheduling
PDF
Fcfs and sjf
Data Warehouse Schema (Star, Snowflake).docx
Correlation Analysis in Machine Learning.pdf
Naive Bayes and Decision Tree Algorithm.pdf
Frequent Pattern Growth Mining Algorithm.pdf
Direct Hashing and Pruning Algorithm in Data MIning.pdf
Association Rule Mining with Apriori Algorithm.pdf
HITS Algorithm in Data and Web MIning.pdf
Page Rank Algorithm in Data Mining and Web Application.pdf
K Nearest Neighbor Classifier in Machine Learning.pdf
K Means Clustering Algorithm in Machine Learning.pdf
2D Transformation in Computer Graphics
3D Transformation in Computer Graphics
Projection
2D Transformation
Line drawing algorithm
Fragmentation
File organization
Bankers algorithm
RR and priority scheduling
Fcfs and sjf

Recently uploaded (20)

PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
Classroom Observation Tools for Teachers
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Pharma ospi slides which help in ospi learning
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
RMMM.pdf make it easy to upload and study
PPTX
GDM (1) (1).pptx small presentation for students
Chinmaya Tiranga quiz Grand Finale.pdf
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Classroom Observation Tools for Teachers
human mycosis Human fungal infections are called human mycosis..pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Orientation - ARALprogram of Deped to the Parents.pptx
Computing-Curriculum for Schools in Ghana
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Weekly quiz Compilation Jan -July 25.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Pharma ospi slides which help in ospi learning
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
RMMM.pdf make it easy to upload and study
GDM (1) (1).pptx small presentation for students

Quick sort

  • 1. Quick Sort Quicksort is a sorting algorithm based on the divide and conquer approach where  An array is divided into subarrays by selecting a pivot element (element selected from the array). While dividing the array, the pivot element should be positioned in such a way that elements less than pivot are kept on the left side and elements greater than pivot are on the right side of the pivot.  The left and right subarrays are also divided using the same approach. This process continues until each subarray contains a single element.  At this point, elements are already sorted. Finally, elements are combined to form a sorted array. Example: 0 1 2 3 4 5 6 7 50=i 30 10 90 80 20 40 70=j Pivot=50 0 1 2 3 4 5 6 7 50 30 10 90=i 80 20 40=j 70 0 1 2 3 4 5 6 7 50 30 10 40=i 80 20 90=j 70 0 1 2 3 4 5 6 7 50 30 10 40 80=i 20=j 90 70 0 1 2 3 4 5 6 7 50 30 10 40 20=i 80=j 90 70 0 1 2 3 4 5 6 7 50 30 10 40 20=j 80=i 90 70 0 1 2 3 4 5 6 7 20 30 10 40 50 80 90 70
  • 2. Left Half 0 1 2 3 20=i 30 10 40=j Pivot=20 0 1 2 3 20 30=i 10=j 40 0 1 2 3 20 10=i 30=j 40 0 1 2 3 20 10=j 30=i 40 0 1 2 3 10 20 30 40 Right Half 5 6 7 80=i 90 70=j Pivot=80 5 6 7 80 90=i 70=j 5 6 7 80 70=i 90=j
  • 3. 5 6 7 80 70=j 90=i 5 6 7 70 80 90 Finally 0 1 2 3 4 5 6 7 10 20 30 40 50 70 80 90 Algorithm QuickSort(l,h) { if(l<h) { j=Partition(l,h); QuickSort(l,j); QuickSort(j+1,h); } } Algorithm Partition(l,h) { Pivot=A[l]; i=l,j=h; while(i<j) { do { i++; }while(A[i]<=pivot); do {