Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

2.

7-QUICK SORT

Quick sort is a fast sorting algorithm used to sort a list of elements. Quick sort
algorithm is invented by C. A. R. Hoare.

The quick sort algorithm attempts to separate the list of elements into two parts and
then sort each part recursively. That means it use divide and conquer strategy. In
quick sort, the partition of the list is performed based on the element called pivot.
Here pivot element is one of the elements in the list.
The list is divided into two partitions such that "all elements to the left of pivot are
smaller than the pivot and all elements to the right of pivot are greater than or equal
to the pivot".

Divide: Rearrange the elements and split arrays into two sub-arrays and an element
in between search that each element in left sub array is less than or equal to the
average element and each element in the right sub- array is larger than the middle
element.
Conquer: Recursively, sort two sub arrays.
Combine: Combine the already sorted array.

It is also called partition-exchange sort. This algorithm divides the list into three
main parts:

▪ Elements less than the Pivot element


▪ Pivot element(Central element)
▪ Elements greater than the pivot element
Pivot element can be any element from the array, it can be the first element, the last
element or any random element. In this tutorial, we will take the rightmost element
or the last element as pivot.

For example: In the array {52, 37, 63, 14, 17, 8, 6, 25}, we take 25 as pivot. So
after the first pass, the list will be changed like this.

{6 8 17 14 25 63 37 52}

Hence after the first pass, pivot will be set at its position, with all the elements
smaller to it on its left and all the elements larger than to its right. Now 6 8 17 14
and 63 37 52 are considered as two separate sunarrays, and same recursive logic
will be applied on them, and we will keep doing this until the complete array is
sorted.
How Does Quick Sort Works?
▪ Quick Sort follows a recursive algorithm.
▪ It divides the given array into two sections using a partitioning element called
as pivot.
The division performed is such that-
● All the elements to the left side of pivot are smaller than pivot.
● All the elements to the right side of pivot are greater than pivot.

After dividing the array into two sections, the pivot is set at its correct position.
Then, sub arrays are sorted separately by applying quick sort algorithm recursively
.
Step by Step Process

In Quick sort algorithm, partitioning of the list is performed using following


steps...
Step 1 - Consider the first element of the list as pivot (i.e., Element at first position
in the list).
Step 2 - Define two variables i and j. Set i and j to first and last elements of the list
respectively.
Step 3 - Increment i until list[i] > pivot then stop.
Step 4 - Decrement j until list[j] < pivot then stop.
Step 5 - If i < j then exchange list[i] and list[j].
Step 6 - Repeat steps 3,4 & 5 until i > j.
Step 7 - Exchange the pivot element with list[j] element.

Quick Sort Example-


 
Consider the following array has to be sorted in ascending order using quick sort
algorithm-
 

 
Quick Sort Algorithm works in the following steps-
 
Step-01:
 
Initially-
● Left and Loc (pivot) points to the first element of the array.
● Right points to the last element of the array.
 
So to begin with, we set loc = 0, left = 0 and right = 5 as-
 

 Step-02:
 
Since loc points at left, so algorithm starts from right and move towards left.
As a[loc] < a[right], so algorithm moves right one position towards left as-
Now, loc = 0, left = 0 and right = 4.
 
Step-03:
 
Since loc points at left, so algorithm starts from right and move towards left.
As a[loc] > a[right], so algorithm swaps a[loc] and a[right] and loc points at right as-
 

 
Now, loc = 4, left = 0 and right = 4.
 
Step-04:
 
Since loc points at right, so algorithm starts from left and move towards right.
As a[loc] > a[left], so algorithm moves left one position towards right as-
 
 
Now, loc = 4, left = 1 and right = 4.
 
Step-05:
 
Since loc points at right, so algorithm starts from left and move towards right.
As a[loc] > a[left], so algorithm moves left one position towards right as-
 

 
Now, loc = 4, left = 2 and right = 4.
 
Step-06:
 
Since loc points at right, so algorithm starts from left and move towards right.
As a[loc] < a[left], so we algorithm swaps a[loc] and a[left] and loc points at left as-
 

 
Now, loc = 2, left = 2 and right = 4.
 
Step-07:
 
Since loc points at left, so algorithm starts from right and move towards left.
As a[loc] < a[right], so algorithm moves right one position towards left as-
 
 
Now, loc = 2, left = 2 and right = 3.
 
Step-08:
 
Since loc points at left, so algorithm starts from right and move towards left.
As a[loc] > a[right], so algorithm swaps a[loc] and a[right] and loc points at right as-
 

 
Now, loc = 3, left = 2 and right = 3.
 
Step-09:
 
Since loc points at right, so algorithm starts from left and move towards right.
As a[loc] > a[left], so algorithm moves left one position towards right as-
 

 
Now, loc = 3, left = 3 and right = 3.
 
Now,
● loc, left and right points at the same element.
● This indicates the termination of procedure.
● The pivot element 25 is placed in its final position.
● All elements to the right side of element 25 are greater than it.
● All elements to the left side of element 25 are smaller than it.
 
 
Now, quick sort algorithm is applied on the left and right sub arrays separately in the
similar manner.

Quick Sort Algorithm

quickSort(array, leftmostIndex, rightmostIndex)


if (leftmostIndex < rightmostIndex)
pivotIndex <- partition(array,leftmostIndex, rightmostIndex)
quickSort(array, leftmostIndex, pivotIndex)
quickSort(array, pivotIndex + 1, rightmostIndex)

partition(array, leftmostIndex, rightmostIndex)


set rightmostIndex as pivotIndex
storeIndex <- leftmostIndex - 1
for i <- leftmostIndex + 1 to rightmostIndex
if element[i] < pivotElement
swap element[i] and element[storeIndex]
storeIndex++
swap pivotElement and element[storeIndex+1]
return storeIndex + 1
Implementaion of Quick Sort Algorithm using C Programming Language

#include<stdio.h>
#include<conio.h>
void quickSort(int [10],int,int);
void main(){
int list[20],size,i;

printf("Enter size of the list: ");


scanf("%d",&size);

printf("Enter %d integer values: ",size);


for(i = 0; i < size; i++)
scanf("%d",&list[i]);

quickSort(list,0,size-1);

printf("List after sorting is: ");


for(i = 0; i < size; i++)
printf(" %d",list[i]);

getch();
}

void quickSort(int list[10],int first,int last){


int pivot,i,j,temp;
if(first < last){
pivot = first;
i = first;
j = last;

while(i < j){


while(list[i] <= list[pivot] && i < last)
i++;
while(list[j] > list[pivot])
j--;
if(i <j){
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}

temp = list[pivot];
list[pivot] = list[j];
list[j] = temp;
quickSort(list,first,j-1);
quickSort(list,j+1,last);
}}
Output

Quick Sort Analysis-


 
▪ To find the location of an element that splits the array into two parts, O(n)
operations are required.
▪ This is because every element in the array is compared to the partitioning
element.
▪ After the division, each section is examined separately.
▪ If the array is split approximately in half (which is not usually), then there will
be log2n splits.
▪ Therefore, total comparisons required are f(n) = n x log2n = O(nlog2n).
▪ Order of Quick Sort = O(nlog2n)
 
 
Worst Case-
 
● Quick Sort is sensitive to the order of input data.
● It gives the worst performance when elements are already in the ascending
order.
● It then divides the array into sections of 1 and (n-1) elements in each call.
● Then, there are (n-1) divisions in all.
● Therefore, here total comparisons required are f(n) = n x (n-1) = O(n2).
● Order of Quick Sort in worst case = O(n2)
  
Advantages of Quick Sort-

 The advantages of quick sort algorithm are-

● Quick Sort is an in-place sort, so it requires no temporary memory.


● Quick Sort is typically faster than other algorithms.(because its inner loop can
be efficiently implemented on most architectures)
● Quick Sort tends to make excellent usage of the memory hierarchy like virtual
memory or caches.
● Quick Sort can be easily parallelized due to its divide and conquer nature.

Disadvantages of Quick Sort-


The disadvantages of quick sort algorithm are-
● The worst case complexity of quick sort is O(n2).
● This complexity is worse than O(nlogn) worst case complexity of algorithms
like merge sort, heap sort etc.
● It is not a stable sort i.e. the order of equal elements may not be preserved.
***********************************************************

You might also like