SlideShare a Scribd company logo
Quicksort Algorithm:
Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two
smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort
the sub-arrays.
The steps are:
The base case of the recursion is arrays of size zero or one, which never need to be sorted.
The pivot selection and partitioning steps can be done in several different ways; the choice of
specific implementation schemes greatly affects the algorithm's performance.
This algorithm is based on Divide and Conquer paradigm. It is implemented using merge sort. In
this approach the time complexity will be O(n log(n)) . Actually in divide step we divide the
problem in two parts. And then two parts are solved recursively. The key concept is two count
the number of inversion in merge procedure. In merge procedure we pass two sub-list. The
element is sorted and inversion is found as follows
a)Divide : Divide the array in two parts a[0] to a[n/2] and a[n/2+1] to a[n].
b)Conquer : Conquer the sub-problem by solving them recursively.
1) Set count=0,0,i=left,j=mid. C is the sorted list.
2) Traverse list1 and list2 until mid element or right element is encountered .
3) Compare list1[i] and list[j].
i) If list1[i]<=list2[j]
c[k++]=list1[i++]
else
c[k++]=list2[j++]
count = count + mid-i;
4) add rest elements of list1 and list2 in c.
5) copy sorted list c back to original list.
6) return count.
void quickSort(int arr[], int left, int right) {
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
/* partition */
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
/* recursion */
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
}
Solution
Quicksort Algorithm:
Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two
smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort
the sub-arrays.
The steps are:
The base case of the recursion is arrays of size zero or one, which never need to be sorted.
The pivot selection and partitioning steps can be done in several different ways; the choice of
specific implementation schemes greatly affects the algorithm's performance.
This algorithm is based on Divide and Conquer paradigm. It is implemented using merge sort. In
this approach the time complexity will be O(n log(n)) . Actually in divide step we divide the
problem in two parts. And then two parts are solved recursively. The key concept is two count
the number of inversion in merge procedure. In merge procedure we pass two sub-list. The
element is sorted and inversion is found as follows
a)Divide : Divide the array in two parts a[0] to a[n/2] and a[n/2+1] to a[n].
b)Conquer : Conquer the sub-problem by solving them recursively.
1) Set count=0,0,i=left,j=mid. C is the sorted list.
2) Traverse list1 and list2 until mid element or right element is encountered .
3) Compare list1[i] and list[j].
i) If list1[i]<=list2[j]
c[k++]=list1[i++]
else
c[k++]=list2[j++]
count = count + mid-i;
4) add rest elements of list1 and list2 in c.
5) copy sorted list c back to original list.
6) return count.
void quickSort(int arr[], int left, int right) {
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
/* partition */
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
/* recursion */
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
}
Ad

More Related Content

Similar to Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf (20)

Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
kalyanineve
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
Tosin Amuda
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
pppepito86
 
Data Structures & Algorithms - Lecture 2
Data Structures & Algorithms - Lecture 2Data Structures & Algorithms - Lecture 2
Data Structures & Algorithms - Lecture 2
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
Dev Chauhan
 
Design and Analysis of Algorithm in Compter Science.pptx
Design and  Analysis of Algorithm in Compter Science.pptxDesign and  Analysis of Algorithm in Compter Science.pptx
Design and Analysis of Algorithm in Compter Science.pptx
rahulshawit2023
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
B.Kirron Reddi
 
Ada notes
Ada notesAda notes
Ada notes
VIKAS SINGH BHADOURIA
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
smruti sarangi
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
RenalthaPujaBagaskar
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
yasser3omr
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
SushantRaj25
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
KamalAlbashiri
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd year
palhimanshi999
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptx
sgrishma559
 
Array 2
Array 2Array 2
Array 2
Abbott
 
Sorting2
Sorting2Sorting2
Sorting2
Saurabh Mishra
 
chapter7.ppt
chapter7.pptchapter7.ppt
chapter7.ppt
Preetiverma538521
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
WrushabhShirsat3
 
chapter7 Sorting.ppt
chapter7 Sorting.pptchapter7 Sorting.ppt
chapter7 Sorting.ppt
Nielmagahod
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
Tosin Amuda
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
pppepito86
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
Dev Chauhan
 
Design and Analysis of Algorithm in Compter Science.pptx
Design and  Analysis of Algorithm in Compter Science.pptxDesign and  Analysis of Algorithm in Compter Science.pptx
Design and Analysis of Algorithm in Compter Science.pptx
rahulshawit2023
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
smruti sarangi
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
yasser3omr
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd year
palhimanshi999
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptx
sgrishma559
 
Array 2
Array 2Array 2
Array 2
Abbott
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
WrushabhShirsat3
 
chapter7 Sorting.ppt
chapter7 Sorting.pptchapter7 Sorting.ppt
chapter7 Sorting.ppt
Nielmagahod
 

More from anupamfootwear (20)

Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 Problem1 java codeimport java.util.Scanner; Java code to pr.pdf Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
anupamfootwear
 
All of the aboveSolution All of the above.pdf
 All of the aboveSolution All of the above.pdf All of the aboveSolution All of the above.pdf
All of the aboveSolution All of the above.pdf
anupamfootwear
 
Yields a colorless solution and a white precipita.pdf
                     Yields a colorless solution and a white precipita.pdf                     Yields a colorless solution and a white precipita.pdf
Yields a colorless solution and a white precipita.pdf
anupamfootwear
 
ROund to one significant figures, THen, its 2..pdf
                     ROund to one significant figures,  THen, its 2..pdf                     ROund to one significant figures,  THen, its 2..pdf
ROund to one significant figures, THen, its 2..pdf
anupamfootwear
 
P=7.7(0.92)^t growth rate is dPdt differentiate .pdf
                     P=7.7(0.92)^t growth rate is dPdt differentiate .pdf                     P=7.7(0.92)^t growth rate is dPdt differentiate .pdf
P=7.7(0.92)^t growth rate is dPdt differentiate .pdf
anupamfootwear
 
option (D) ... reason posted in my previous answe.pdf
                     option (D) ... reason posted in my previous answe.pdf                     option (D) ... reason posted in my previous answe.pdf
option (D) ... reason posted in my previous answe.pdf
anupamfootwear
 
no.of moles of H2moles=28.82=14.4moles no.of mol.pdf
                     no.of moles of H2moles=28.82=14.4moles no.of mol.pdf                     no.of moles of H2moles=28.82=14.4moles no.of mol.pdf
no.of moles of H2moles=28.82=14.4moles no.of mol.pdf
anupamfootwear
 
Ionic Compounds Think of an ionic compound as a .pdf
                     Ionic Compounds Think of an ionic compound as a .pdf                     Ionic Compounds Think of an ionic compound as a .pdf
Ionic Compounds Think of an ionic compound as a .pdf
anupamfootwear
 
In the First molecule there will be Resonance whi.pdf
                     In the First molecule there will be Resonance whi.pdf                     In the First molecule there will be Resonance whi.pdf
In the First molecule there will be Resonance whi.pdf
anupamfootwear
 
He down the group, IE decreases. Solution.pdf
                     He   down the group, IE decreases.   Solution.pdf                     He   down the group, IE decreases.   Solution.pdf
He down the group, IE decreases. Solution.pdf
anupamfootwear
 
Uncouple agents will never disrupt the electron transport (they will.pdf
Uncouple agents will never disrupt the electron transport (they will.pdfUncouple agents will never disrupt the electron transport (they will.pdf
Uncouple agents will never disrupt the electron transport (they will.pdf
anupamfootwear
 
The N and O atoms are both sp2 hybridized.The sp2 hybrid orbitals .pdf
The N and O atoms are both sp2 hybridized.The sp2 hybrid orbitals .pdfThe N and O atoms are both sp2 hybridized.The sp2 hybrid orbitals .pdf
The N and O atoms are both sp2 hybridized.The sp2 hybrid orbitals .pdf
anupamfootwear
 
d.Addition of sulfuric acid to copper(II) oxide p.pdf
                     d.Addition of sulfuric acid to copper(II) oxide p.pdf                     d.Addition of sulfuric acid to copper(II) oxide p.pdf
d.Addition of sulfuric acid to copper(II) oxide p.pdf
anupamfootwear
 
The 2nd statement and 3rd statement follow the seed and soil theory .pdf
The 2nd statement and 3rd statement follow the seed and soil theory .pdfThe 2nd statement and 3rd statement follow the seed and soil theory .pdf
The 2nd statement and 3rd statement follow the seed and soil theory .pdf
anupamfootwear
 
S2F6SolutionS2F6.pdf
S2F6SolutionS2F6.pdfS2F6SolutionS2F6.pdf
S2F6SolutionS2F6.pdf
anupamfootwear
 
Pictures are not legible, could you plz post it again.Solution.pdf
Pictures are not legible, could you plz post it again.Solution.pdfPictures are not legible, could you plz post it again.Solution.pdf
Pictures are not legible, could you plz post it again.Solution.pdf
anupamfootwear
 
Carbonic acid leaves the soda solution as CO2 (ca.pdf
                     Carbonic acid leaves the soda solution as CO2 (ca.pdf                     Carbonic acid leaves the soda solution as CO2 (ca.pdf
Carbonic acid leaves the soda solution as CO2 (ca.pdf
anupamfootwear
 
C. III S.pdf
                     C. III                                      S.pdf                     C. III                                      S.pdf
C. III S.pdf
anupamfootwear
 
mass= density volume= 2.330.10.10.01 = 2.3310-4gmsatomic .pdf
mass= density volume= 2.330.10.10.01 = 2.3310-4gmsatomic .pdfmass= density volume= 2.330.10.10.01 = 2.3310-4gmsatomic .pdf
mass= density volume= 2.330.10.10.01 = 2.3310-4gmsatomic .pdf
anupamfootwear
 
John is suffering from fifth disease.It is caused by an airborne v.pdf
John is suffering from fifth disease.It is caused by an airborne v.pdfJohn is suffering from fifth disease.It is caused by an airborne v.pdf
John is suffering from fifth disease.It is caused by an airborne v.pdf
anupamfootwear
 
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 Problem1 java codeimport java.util.Scanner; Java code to pr.pdf Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
anupamfootwear
 
All of the aboveSolution All of the above.pdf
 All of the aboveSolution All of the above.pdf All of the aboveSolution All of the above.pdf
All of the aboveSolution All of the above.pdf
anupamfootwear
 
Yields a colorless solution and a white precipita.pdf
                     Yields a colorless solution and a white precipita.pdf                     Yields a colorless solution and a white precipita.pdf
Yields a colorless solution and a white precipita.pdf
anupamfootwear
 
ROund to one significant figures, THen, its 2..pdf
                     ROund to one significant figures,  THen, its 2..pdf                     ROund to one significant figures,  THen, its 2..pdf
ROund to one significant figures, THen, its 2..pdf
anupamfootwear
 
P=7.7(0.92)^t growth rate is dPdt differentiate .pdf
                     P=7.7(0.92)^t growth rate is dPdt differentiate .pdf                     P=7.7(0.92)^t growth rate is dPdt differentiate .pdf
P=7.7(0.92)^t growth rate is dPdt differentiate .pdf
anupamfootwear
 
option (D) ... reason posted in my previous answe.pdf
                     option (D) ... reason posted in my previous answe.pdf                     option (D) ... reason posted in my previous answe.pdf
option (D) ... reason posted in my previous answe.pdf
anupamfootwear
 
no.of moles of H2moles=28.82=14.4moles no.of mol.pdf
                     no.of moles of H2moles=28.82=14.4moles no.of mol.pdf                     no.of moles of H2moles=28.82=14.4moles no.of mol.pdf
no.of moles of H2moles=28.82=14.4moles no.of mol.pdf
anupamfootwear
 
Ionic Compounds Think of an ionic compound as a .pdf
                     Ionic Compounds Think of an ionic compound as a .pdf                     Ionic Compounds Think of an ionic compound as a .pdf
Ionic Compounds Think of an ionic compound as a .pdf
anupamfootwear
 
In the First molecule there will be Resonance whi.pdf
                     In the First molecule there will be Resonance whi.pdf                     In the First molecule there will be Resonance whi.pdf
In the First molecule there will be Resonance whi.pdf
anupamfootwear
 
He down the group, IE decreases. Solution.pdf
                     He   down the group, IE decreases.   Solution.pdf                     He   down the group, IE decreases.   Solution.pdf
He down the group, IE decreases. Solution.pdf
anupamfootwear
 
Uncouple agents will never disrupt the electron transport (they will.pdf
Uncouple agents will never disrupt the electron transport (they will.pdfUncouple agents will never disrupt the electron transport (they will.pdf
Uncouple agents will never disrupt the electron transport (they will.pdf
anupamfootwear
 
The N and O atoms are both sp2 hybridized.The sp2 hybrid orbitals .pdf
The N and O atoms are both sp2 hybridized.The sp2 hybrid orbitals .pdfThe N and O atoms are both sp2 hybridized.The sp2 hybrid orbitals .pdf
The N and O atoms are both sp2 hybridized.The sp2 hybrid orbitals .pdf
anupamfootwear
 
d.Addition of sulfuric acid to copper(II) oxide p.pdf
                     d.Addition of sulfuric acid to copper(II) oxide p.pdf                     d.Addition of sulfuric acid to copper(II) oxide p.pdf
d.Addition of sulfuric acid to copper(II) oxide p.pdf
anupamfootwear
 
The 2nd statement and 3rd statement follow the seed and soil theory .pdf
The 2nd statement and 3rd statement follow the seed and soil theory .pdfThe 2nd statement and 3rd statement follow the seed and soil theory .pdf
The 2nd statement and 3rd statement follow the seed and soil theory .pdf
anupamfootwear
 
Pictures are not legible, could you plz post it again.Solution.pdf
Pictures are not legible, could you plz post it again.Solution.pdfPictures are not legible, could you plz post it again.Solution.pdf
Pictures are not legible, could you plz post it again.Solution.pdf
anupamfootwear
 
Carbonic acid leaves the soda solution as CO2 (ca.pdf
                     Carbonic acid leaves the soda solution as CO2 (ca.pdf                     Carbonic acid leaves the soda solution as CO2 (ca.pdf
Carbonic acid leaves the soda solution as CO2 (ca.pdf
anupamfootwear
 
mass= density volume= 2.330.10.10.01 = 2.3310-4gmsatomic .pdf
mass= density volume= 2.330.10.10.01 = 2.3310-4gmsatomic .pdfmass= density volume= 2.330.10.10.01 = 2.3310-4gmsatomic .pdf
mass= density volume= 2.330.10.10.01 = 2.3310-4gmsatomic .pdf
anupamfootwear
 
John is suffering from fifth disease.It is caused by an airborne v.pdf
John is suffering from fifth disease.It is caused by an airborne v.pdfJohn is suffering from fifth disease.It is caused by an airborne v.pdf
John is suffering from fifth disease.It is caused by an airborne v.pdf
anupamfootwear
 
Ad

Recently uploaded (20)

"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx   quiz by Ridip HazarikaTHE STG QUIZ GROUP D.pptx   quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
Ridip Hazarika
 
Real GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for SuccessReal GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for Success
Mark Soia
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx   quiz by Ridip HazarikaTHE STG QUIZ GROUP D.pptx   quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
Ridip Hazarika
 
Real GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for SuccessReal GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for Success
Mark Soia
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Ad

Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf

  • 1. Quicksort Algorithm: Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays. The steps are: The base case of the recursion is arrays of size zero or one, which never need to be sorted. The pivot selection and partitioning steps can be done in several different ways; the choice of specific implementation schemes greatly affects the algorithm's performance. This algorithm is based on Divide and Conquer paradigm. It is implemented using merge sort. In this approach the time complexity will be O(n log(n)) . Actually in divide step we divide the problem in two parts. And then two parts are solved recursively. The key concept is two count the number of inversion in merge procedure. In merge procedure we pass two sub-list. The element is sorted and inversion is found as follows a)Divide : Divide the array in two parts a[0] to a[n/2] and a[n/2+1] to a[n]. b)Conquer : Conquer the sub-problem by solving them recursively. 1) Set count=0,0,i=left,j=mid. C is the sorted list. 2) Traverse list1 and list2 until mid element or right element is encountered . 3) Compare list1[i] and list[j]. i) If list1[i]<=list2[j] c[k++]=list1[i++] else c[k++]=list2[j++] count = count + mid-i; 4) add rest elements of list1 and list2 in c. 5) copy sorted list c back to original list. 6) return count. void quickSort(int arr[], int left, int right) { int i = left, j = right; int tmp; int pivot = arr[(left + right) / 2]; /* partition */ while (i <= j) { while (arr[i] < pivot) i++; while (arr[j] > pivot)
  • 2. j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } }; /* recursion */ if (left < j) quickSort(arr, left, j); if (i < right) quickSort(arr, i, right); } Solution Quicksort Algorithm: Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays. The steps are: The base case of the recursion is arrays of size zero or one, which never need to be sorted. The pivot selection and partitioning steps can be done in several different ways; the choice of specific implementation schemes greatly affects the algorithm's performance. This algorithm is based on Divide and Conquer paradigm. It is implemented using merge sort. In this approach the time complexity will be O(n log(n)) . Actually in divide step we divide the problem in two parts. And then two parts are solved recursively. The key concept is two count the number of inversion in merge procedure. In merge procedure we pass two sub-list. The element is sorted and inversion is found as follows a)Divide : Divide the array in two parts a[0] to a[n/2] and a[n/2+1] to a[n]. b)Conquer : Conquer the sub-problem by solving them recursively. 1) Set count=0,0,i=left,j=mid. C is the sorted list. 2) Traverse list1 and list2 until mid element or right element is encountered . 3) Compare list1[i] and list[j].
  • 3. i) If list1[i]<=list2[j] c[k++]=list1[i++] else c[k++]=list2[j++] count = count + mid-i; 4) add rest elements of list1 and list2 in c. 5) copy sorted list c back to original list. 6) return count. void quickSort(int arr[], int left, int right) { int i = left, j = right; int tmp; int pivot = arr[(left + right) / 2]; /* partition */ while (i <= j) { while (arr[i] < pivot) i++; while (arr[j] > pivot) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } }; /* recursion */ if (left < j) quickSort(arr, left, j); if (i < right) quickSort(arr, i, right); }