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

More Related Content

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

PPT
quick_sort_with_explanationandImplmentation.ppt
MohamedWael807163
 
PPTX
Quick Sort in data structure.pptx
ujjwalmatoliya
 
PPTX
Sortings .pptx
MuhammadSheraz836877
 
PPTX
Quick-Sort Algorithm and pivot selection
SubhranjaliBehera
 
PDF
Quick sort
Abdelrahman Saleh
 
PPTX
Divide-and-conquer
Mrunal Patil
 
PPTX
Module 2 Quick Sort algorithm in analysis of algorithm.pptx
SiddhantRaut20
 
PDF
Sorting Algorithms and their implementations
ChakravarthiMusic1
 
PPTX
Quick sort by Sania Nisar
Sania Nisar
 
PDF
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
PPTX
Quick sort.pptx
Anbarasan Radhakrishnan R
 
PPTX
Merge sort and quick sort
Shakila Mahjabin
 
PPTX
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
LakshmiSamivel
 
PPT
Lec35
Nikhil Chilwant
 
DOCX
Assignment 2 data structures National University Of Modern Languages
itsaif1312
 
PPT
Quick sort
Rajendran
 
PPTX
quick and merge.pptx
LakshayYadav46
 
PPTX
CSE680-07QuickSort.pptx
DeepakM509554
 
PPTX
Unit 2 - Quick Sort.pptx
CO1IShwetaKidile
 
quick_sort_with_explanationandImplmentation.ppt
MohamedWael807163
 
Quick Sort in data structure.pptx
ujjwalmatoliya
 
Sortings .pptx
MuhammadSheraz836877
 
Quick-Sort Algorithm and pivot selection
SubhranjaliBehera
 
Quick sort
Abdelrahman Saleh
 
Divide-and-conquer
Mrunal Patil
 
Module 2 Quick Sort algorithm in analysis of algorithm.pptx
SiddhantRaut20
 
Sorting Algorithms and their implementations
ChakravarthiMusic1
 
Quick sort by Sania Nisar
Sania Nisar
 
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
Quick sort.pptx
Anbarasan Radhakrishnan R
 
Merge sort and quick sort
Shakila Mahjabin
 
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
LakshmiSamivel
 
Assignment 2 data structures National University Of Modern Languages
itsaif1312
 
Quick sort
Rajendran
 
quick and merge.pptx
LakshayYadav46
 
CSE680-07QuickSort.pptx
DeepakM509554
 
Unit 2 - Quick Sort.pptx
CO1IShwetaKidile
 

More from anupamfootwear (20)

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

Recently uploaded (20)

PPTX
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PPTX
GENERAL BIOLOGY 1 - Subject Introduction
marvinnbustamante1
 
PDF
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PDF
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
PDF
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
PPTX
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
PPTX
Different types of inheritance in odoo 18
Celine George
 
PPTX
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
PPTX
Building Powerful Agentic AI with Google ADK, MCP, RAG, and Ollama.pptx
Tamanna36
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PPTX
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
PDF
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
PPTX
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PPTX
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
PDF
I3PM Case study smart parking 2025 with uptoIP® and ABP
MIPLM
 
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
GENERAL BIOLOGY 1 - Subject Introduction
marvinnbustamante1
 
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
Different types of inheritance in odoo 18
Celine George
 
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
Building Powerful Agentic AI with Google ADK, MCP, RAG, and Ollama.pptx
Tamanna36
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Introduction presentation of the patentbutler tool
MIPLM
 
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
I3PM Case study smart parking 2025 with uptoIP® and ABP
MIPLM
 

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