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

PPTX
Quick sort
PPTX
09 QUICK SORT Design and Analysis of algorithms
PPTX
MERGE and Quick Sort algorithm explain ppt
PPT
Sorting algos > Data Structures & Algorithums
PPT
Data Structure Sorting
PPTX
Divide and conquer - Quick sort
PPT
quicksort (1).ppt
PPT
s4_quick_sort.ppt
Quick sort
09 QUICK SORT Design and Analysis of algorithms
MERGE and Quick Sort algorithm explain ppt
Sorting algos > Data Structures & Algorithums
Data Structure Sorting
Divide and conquer - Quick sort
quicksort (1).ppt
s4_quick_sort.ppt

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

PPT
quick_sort_with_explanationandImplmentation.ppt
PPTX
Quick Sort in data structure.pptx
PPTX
Sortings .pptx
PPTX
Quick-Sort Algorithm and pivot selection
PDF
Quick sort
PPTX
Divide-and-conquer
PPTX
Module 2 Quick Sort algorithm in analysis of algorithm.pptx
PDF
Sorting Algorithms and their implementations
PPTX
Quick sort by Sania Nisar
PDF
Class13_Quicksort_Algorithm.pdf
PPTX
Quick sort.pptx
PPTX
Merge sort and quick sort
PPTX
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
PPT
DOCX
Assignment 2 data structures National University Of Modern Languages
PPT
Quick sort
PPTX
quick and merge.pptx
PPTX
CSE680-07QuickSort.pptx
PPTX
Unit 2 - Quick Sort.pptx
quick_sort_with_explanationandImplmentation.ppt
Quick Sort in data structure.pptx
Sortings .pptx
Quick-Sort Algorithm and pivot selection
Quick sort
Divide-and-conquer
Module 2 Quick Sort algorithm in analysis of algorithm.pptx
Sorting Algorithms and their implementations
Quick sort by Sania Nisar
Class13_Quicksort_Algorithm.pdf
Quick sort.pptx
Merge sort and quick sort
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
Assignment 2 data structures National University Of Modern Languages
Quick sort
quick and merge.pptx
CSE680-07QuickSort.pptx
Unit 2 - Quick Sort.pptx

More from anupamfootwear (20)

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

Recently uploaded (20)

PDF
Classroom Observation Tools for Teachers
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Basic Mud Logging Guide for educational purpose
PDF
01-Introduction-to-Information-Management.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
master seminar digital applications in india
PDF
RMMM.pdf make it easy to upload and study
PDF
Computing-Curriculum for Schools in Ghana
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Lesson notes of climatology university.
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Classroom Observation Tools for Teachers
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Sports Quiz easy sports quiz sports quiz
Basic Mud Logging Guide for educational purpose
01-Introduction-to-Information-Management.pdf
Insiders guide to clinical Medicine.pdf
Pharma ospi slides which help in ospi learning
Anesthesia in Laparoscopic Surgery in India
Pharmacology of Heart Failure /Pharmacotherapy of CHF
master seminar digital applications in india
RMMM.pdf make it easy to upload and study
Computing-Curriculum for Schools in Ghana
human mycosis Human fungal infections are called human mycosis..pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Lesson notes of climatology university.
O5-L3 Freight Transport Ops (International) V1.pdf
GDM (1) (1).pptx small presentation for students
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

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