Classification of Sorting Algorithms

Sorting is an algorithm which arranges the elements of a given list in a particular order [ascending or descending].

Sorting algorithms are categorized on the following basis –

**By number of comparisons :**

Comparison-based sorting algorithms check the elements of the list by key comparison operation and need at least O(n log n) comparisons for most inputs. In this method, algorithms are classified based on the number of comparisons. For comparison based sorting algorithms, best case behavior is O(n log n) and worst case behavior is O(n2). For example – Counting sort, Bucket sort, Radix sort, etc.

**By Number of Swaps :**

In this method, sorting algorithms are categorized by the number of swaps (interchanging of position of to numbers, also called inversion).

**By Memory Usage :**

Some sorting algorithms are “in place” and they need O(1) or O(log n) memory to create auxiliary locations for sorting the data temporarily.

**By Recursion :**

Sorting algorithms are either recursive (for example – quick sort) or non-recursive (for example – selection sort, and insertion sort), and there are some algorithms which use both (for example – merge sort).

**By Stability :**

Sorting algorithm is stable if two elements with equal values appear in the same order in output as it was in the input. The stability of a sorting algorithm can be checked with how it treats equal elements. Stable algorithms preserve the relative order of equal elements, while unstable sorting algorithms don’t. In other words, stable sorting maintains the position of two equals elements similar to one another. For example – Insertion Sort, Bubble Sort , and Radix Sort.

**By Adaptability :**

In a few sorting algorithms, the complexity changes based on pre-sorted input i.e. pre-sorted array of the input affects the running time. The algorithms that take this adaptability into account are known to be adaptive algorithms. For example – Quick sort is an adaptive sorting algorithm because the time complexity of Quick sort depends on the initial input sequence. If input is already sorted then time complexity becomes O(n^2) and if input sequence is not sorted then time complexity becomes O(n logn).Some adaptive sorting algorithms are : Bubble Sort, Insertion Sort and Quick Sort. On the other hand some non-adaptive sorting algorithms are : Selection Sort, Merge Sort, and Heap Sort.

**Internal Sorting :**

Sorting algorithms that use main memory exclusively during the sort are called internal sorting algorithms. This kind of algorithm assumes high-speed random access to all memory. Some of the common algorithms that use this sorting feature are : Bubble Sort, Insertion Sort., and Quick Sort.

**External Sorting :**

Sorting algorithms that use external memory, during the sorting come under this category. They are comparatively slower than internal sorting algorithms. For example merge sort algorithm. It sorts chunks that each fit in RAM, then merges the sorted chunks together.

Attention reader! Don’t stop learning now. Learn all **GATE CS concepts with Free Live Classes** on our youtube channel.