Categories: Algorithms

Complexities of Basic Sorting Algorithms

Heads Up! Please don’t learn this table by heart. This is just to check your understanding and you should know how these complexities came

 

AlgorithmWorst CaseAverage CaseBest CaseMin. no. of swapsMax. no. of swaps
Bubble$\Theta(n^{2})$$\Theta(n^{2})$$\Theta(n)$0$\Theta(n^{2})$
Selection$\Theta(n^{2})$$\Theta(n^{2})$$\Theta(n^{2})$0$\Theta(n)$
Insertion$\Theta(n^{2})$$\Theta(n^{2})$$\Theta(n)$0$\Theta(n^{2})$
Quick$\Theta(n^{2})$$\Theta(n\lg n)$$\Theta(n\lg n)$0$\Theta(n^{2})$
Merge$\Theta(n\lg n)$$\Theta(n\lg n)$$\Theta(n\lg n)$Is not in-place sortingIs not in-place sorting
Heap$\Theta(n\lg n)$$\Theta(n\lg n)$$\Theta(n\lg n)$$O(n\lg n)$$\Theta(n\lg n)$
The best case for insertion sort happens when the input array is already sorted. Each new element will then be put at the end of the array and the complexity will be $\Theta(n)$.
Interestingly, the worst case for quick sort also happens when the input array is sorted (either ascending or descending). This happens only if we choose the pivot as either the first element or the last element. In either of these case, we split the array across the pivot with n-1 elements on one array and just 1 element on the other array, which makes the complexity go to $\Theta(n^2)$

missinglatex

In-Place Sorting

When sorting happens within the same array, its called in-place sorting. All the above sorting algorithms except merge sort are in-place sorting. In merge sort, we use an auxiliary array for merging two sub-arrays and hence its not in-place sorting.

Stable sorting

This matters only for arrays with some element(s) repeated. If $A[x] = A[y]$, in the original array such that $x<y$, then in the sorted array, $A[x]$ must be placed at position $u$ and $A[y]$ must be placed at position $v$, such that $u < v$. i.e.; the relative positions of same value elements must be same in the sorted array as in the input array, for the sorting algorithm to be stable. In most cases stability depends on the way in which algorithm is implemented. Still, its not possible to implement heap sort as a stable sort.

 

 

arjun

Share
Published by
arjun

Recent Posts

GATE CSE 2022 Admissions, Results and Placement Responses

This page shows all details regarding GATE CSE 2022 Admissions including results, admission offers and…

2 years ago

GATE CSE 2021 Admission Responses

Source Add your Response Rank Predictor for GATE CSE 2021 Result Responses: GATE CSE 2021…

3 years ago

GATE CSE Books – More Options

Best Books for GATE CSE with Relevant Chapters to Read  Heads Up! These GATE CSE…

3 years ago

ISI PCB Previous Year Papers with Solution

Indian Statistical Institute(ISI) offers M Tech in Computer Science with the Admission Test Codes MMA/PCA…

3 years ago

ISI JRF Previous Year Papers with Solution

Indian Statistical Institute(ISI) offers Junior Research Fellowships (JRF) in Computer Science, Statistics, Mathematics, Quantitative Economics,…

3 years ago

ISI DCG Previous Year Papers with Solution

 Indian Statistical Institute(ISI) conduct the admission test for Postgraduate Diploma in Computer Applications(PGDCA) with the…

3 years ago