SlideShare a Scribd company logo
1 of 3
Download to read offline
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);
}

More Related Content

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

DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDev Chauhan
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1smruti sarangi
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).pptyasser3omr
 
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 yearpalhimanshi999
 
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.pptxsgrishma559
 
Array 2
Array 2Array 2
Array 2Abbott
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufWrushabhShirsat3
 
chapter7 Sorting.ppt
chapter7 Sorting.pptchapter7 Sorting.ppt
chapter7 Sorting.pptNielmagahod
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms ArraysManishPrajapati78
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquerchidabdu
 

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

DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
Ada notes
Ada notesAda notes
Ada notes
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
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
 
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
 
Array 2
Array 2Array 2
Array 2
 
Sorting2
Sorting2Sorting2
Sorting2
 
chapter7.ppt
chapter7.pptchapter7.ppt
chapter7.ppt
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
 
chapter7 Sorting.ppt
chapter7 Sorting.pptchapter7 Sorting.ppt
chapter7 Sorting.ppt
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
 
Merge sort
Merge sortMerge sort
Merge sort
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
 

More from 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.pdfanupamfootwear
 
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.pdfanupamfootwear
 
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.pdfanupamfootwear
 
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..pdfanupamfootwear
 
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 .pdfanupamfootwear
 
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.pdfanupamfootwear
 
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.pdfanupamfootwear
 
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 .pdfanupamfootwear
 
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.pdfanupamfootwear
 
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.pdfanupamfootwear
 
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.pdfanupamfootwear
 
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 .pdfanupamfootwear
 
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.pdfanupamfootwear
 
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 .pdfanupamfootwear
 
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.pdfanupamfootwear
 
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.pdfanupamfootwear
 
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 .pdfanupamfootwear
 
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.pdfanupamfootwear
 

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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
S2F6SolutionS2F6.pdf
S2F6SolutionS2F6.pdfS2F6SolutionS2F6.pdf
S2F6SolutionS2F6.pdf
 
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
 
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
 
C. III S.pdf
                     C. III                                      S.pdf                     C. III                                      S.pdf
C. III S.pdf
 
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
 
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
 

Recently uploaded

Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 

Recently uploaded (20)

Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

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); }