Sorting Algorithms
Last Updated :
14 Apr, 2025
Improve
A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order.
- There exist different sorting algorithms for different different types of inputs, for example a binary array, a character array, an array with a large range of values or an array with many duplicates or a small vs large array.
- The algorithms may also differ according to output requirements. For example, stable sorting (or maintains original order of equal elements) or not stable.
- Sorting is provided in library implementation of most of the programming languages. These sorting functions typically are general purpose functions with flexibility of providing the expected sorting order (increasing or decreasing or by a specific key in case of objects).
Basics of Sorting Algorithms:
Sorting Algorithms:
Comparison Based : Selection Sort, Bubble Sort, Insertion Sort, Merge Sort, Quick Sort, Heap Sort, Cycle Sort, 3-way Merge Sort
Non Comparison Based : Counting Sort, Radix Sort, Bucket Sort, TimSort, Comb Sort, Pigeonhole Sort
Hybrid Sorting Algorithms : IntroSort, Tim Sort
Library Implementations:
- qsort() in C
- sort() in C++ STL
- Arrays.sort() in Java with examples
- Collections.sort() in Java with Examples
- Sort a List in Python
- Sorting in JavaScript
Easy Problems on Sorting:
- Check if an array is Sorted
- Sort an array of two types
- Sort a String
- Sort Each Row of a Matrix
- Sort a Matrix
- Sort a Linked List
- Sort in Wave Form
- Sort by Frequency
- Sort from Different Machines
- Check if any two intervals overlap
- Missing elements of a range
- Sort by set bits counts
- Sort even and odd placed in different orders
- Sorting Big Integers
- Sort strings by lengths
- Merge Two Sorted Arrays
- Sort when two halves are sorted
- 2 Sum - Pair in a Sorted Array
- Intersection of two sorted arrays
- Union of two sorted arrays
- Meeting Rooms
Medium Problems on Sorting:
- Minimum Increments to Make Unique
- Merge Overlapping Intervals
- Minimum Platforms
- Closest Pair of Elements
- Closest Pair of Points
- Chocolate Distribution Problem
- Min and Max Amount to Buy All
- Three Way Partitioning
- Sort an array of 0s, 1s and 2s
- Sort a linked list of 0s, 1s and 2s
- Inversion count
- K-th Smallest Element
- K Smallest Elements
- 3 Sum - Find Any
- 3 Sum - Closest Triplet
- Smallest Difference Triplet from Three arrays
- Merge K Sorted Arrays
- Merge K Sorted Linked Lists
- Min Unsorted Subarray to make array sorted
- Sort a nearly sorted array
- Sort n numbers in range from 0 to n^2 – 1
- Sort an array of 1 to n
- Sort according to order defined by another
- Maximum intervals overlap
- Permutation with worst Case of Merge Sort
- Minimum swaps to make two arrays identical
- Permute two arrays such that all pair suns are greater than K
- Bucket Sort To Sort an Array with Negative Numbers
- Convert an Array to reduced form using Vector of pairs
- Check if array can be sorted with conditional swapping of adjacent
- 4 Sum - Find Any [More problems an 4 Sum are in Hard Section]
Hard Problems on Sorting:
- Merge Without Extra Space
- Top K Frequent Elements
- 3 Sum - Distinct Triplets
- 4 Sum - Distinct Quadruples
- 4 Sum - All Quadruples
- 4 Sum - Closest Quadruple
- Surpasser Counts in an Array
- Count distinct occurrences as a subsequence
- Minimum consecutive number subsets
- Minimum swaps for Binary Tree to BST
- K-th smallest element after removing some integers from natural numbers
- Max frequency diff such greater freq item is also is also greater
- Min swaps to reach permuted array with at most 2 positions left swaps allowed
- Making Array Elements Same
- Sort an array after applying an equation
- Array of strings in sorted order without copying strings
Quick Links :
Recommended: