0% found this document useful (0 votes)
2 views

Quick Sort

Quick sort is an efficient sorting algorithm that uses a divide and conquer approach to partition an array into smaller sub-arrays based on a pivot value. The algorithm has a time complexity of O(n*logn) in the best and average cases, but can degrade to O(n^2) in the worst case. While it is fast and has a low space complexity, quick sort is unstable and can perform poorly with certain pivot selections.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Quick Sort

Quick sort is an efficient sorting algorithm that uses a divide and conquer approach to partition an array into smaller sub-arrays based on a pivot value. The algorithm has a time complexity of O(n*logn) in the best and average cases, but can degrade to O(n^2) in the worst case. While it is fast and has a low space complexity, quick sort is unstable and can perform poorly with certain pivot selections.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

QUICK SORT

Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data
into smaller arrays. A large array is partitioned into two arrays one of which holds values
smaller than the specified value, say pivot, based on which the partition is made and another
array holds values greater than the pivot value.

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.

Conquer: Recursively, sort two subarrays with Quicksort

Combine: Combine the already sorted 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:

QUICKSORT (array A, start, end)


{
if (start < end)
{
p = partition(A, start, end)
QUICKSORT (A, start, p - 1)
QUICKSORT (A, p + 1, end)
}
}

Notes compiled by:


Sangita Vishwakarma
DPGITM
Partition Algorithm:
The partition algorithm rearranges the sub-arrays in a place.
PARTITION (array A, start, end)
{
pivot = A[start]
i = start + 1
j = end
while(i<j)
{
while(A[i] <=pivot)
i++;
while(A[j] > pivot)
j++;
if(i<j)
swap (A[i], A[j])
}
swap(A[start], A[j])
return j;
}

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

Notes compiled by:


Sangita Vishwakarma
DPGITM
already in ascending or descending order. The worst-case time complexity of
quicksort is O(n2).

Quicksort Advantages

 It works rapidly and effectively.


 It has the best time complexity when compared to other sorting algorithms.
 Quicksort has a space complexity of O(logn), making it an excellent choice for
situations when space is limited.
Quicksort Disadvantages

 This sorting technique is considered unstable since it doesn’t maintain the key-value
pairs initial order.
 It isn’t as effective when the pivot element is the largest or smallest, or when all of the
components have the same size. The performance of the quicksort is significantly
impacted by these worst-case scenarios.
 It’s difficult to implement since it’s a recursive process, especially if recursion isn’t
available.

Notes compiled by:


Sangita Vishwakarma
DPGITM

You might also like