Sorting is a basic operation when solving algorithmic problems. There are several types of sorting algorithms such as selection sort, bubble sort, quicksort, mergesort, etc ( Sorting Algorithms).
We can use sorting to help us in many cases, such as:
- Makes Lookup Faster: When we sort elements (say in an array), then that allows us to use binary search to do faster lookups.
- Merge and Divide-and-Conquer Algorithms: Many algorithms such as merge sort and quicksort, use sorting as a fundamental step. Sorting is used to divide problem into smaller subproblems, which are then solved independently and combined. There are many problems that are based on merge sort.
- Sorting with a custom sorting logic: Many problems require that we do sorting but the sorting has to be done in a specific way. Alternatively, the sorting can have several cases e.g. sort a queue of objects with different sizes but when sizes are same, sort them by color.