Quick Sort Algorithm
Quick Sort Algorithm
2. Develop a program and measure the running time for Quick Sort with Divide and
Conquer
Aim: To develop a program and measure the running time for Quick Sort with Divide and
Conquer.
Quick Sort:
Sorting is a way of arranging items in a systematic manner. Quicksort is the
widely used sorting algorithm that makes n log n comparisons in average
case for sorting an array of n elements. It is a faster and highly efficient
sorting algorithm. This algorithm follows the divide and conquer approach.
Divide and conquer is a technique of breaking down the algorithms into
subproblems, then solving the subproblems, and combining the results back
together to solve the original problem.
Divide: In Divide, first pick a pivot element. After that, partition or rearrange
the array into two sub-arrays such that each element in the left sub-array is
less than or equal to the pivot element and each element in the right sub-array
is larger than the pivot element.
Quicksort picks an element as pivot, and then it partitions the given array
around the picked pivot element. In quick sort, a large array is divided into
two arrays in which one holds values that are smaller than the specified value
(Pivot), and another array holds the values that are greater than the pivot.
After that, left and right sub-arrays are also partitioned using the same
approach. It will continue until the single element remains in the sub-array.
Choosing the pivot
Picking a good pivot is necessary for the fast implementation of quicksort.
However, it is typical to determine a good pivot. Some of the ways of
choosing a pivot are as follows -
o Pivot can be random, i.e. select the random pivot from the given array.
o Pivot can either be the rightmost element of the leftmost element of the
given array.
o Select median as the pivot element.
Algorithm
Partition Algorithm:
Quicksort complexity
1. Time Complexity
Case Time Complexity
Best Case O(n*logn)
Average Case O(n*logn)
Worst Case O(n2)
o Best Case Complexity - In Quicksort, the best-case occurs when the
pivot element is the middle element or near to the middle element. The
best-case time complexity of quicksort is O(n*logn).
o Average Case Complexity - It occurs when the array elements are in
jumbled order that is not properly ascending and not properly
descending. The average case time complexity of quicksort
is O(n*logn).
o Worst Case Complexity - In quick sort, worst case occurs when the
pivot element is either greatest or smallest element. Suppose, if the
pivot element is always the last element of the array, the worst case
would occur when the given array is sorted already in ascending or
descending order. The worst-case time complexity of quicksort
is O(n2).
2. Space Complexity
Space Complexity O(n*logn)
#include <stdio.h>
int partition (int a[], int start, int end)
{
int pivot = a[end]; // pivot element
int i = (start - 1);
for (int j = start; j <= end - 1; j++)
{
// If current element is smaller than the pivot
if (a[j] < pivot)
{
i++; // increment index of smaller element
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
}