Algorithms · Elementary Sorting Algorithms
Introduction to Sorting Problem
Overview
The sorting problem asks for any sequence of numbers to be rearranged into ascending or descending order. A correct but impractical brute-force method exists (trying all permutations), and its exponential cost is exactly why better algorithms such as insertion sort are needed.
Key concepts
- Sorting problem: Given any sequence (list/array) of numbers, rearrange them so that the output is in either ascending order or descending order.
- Input: any random array / random list of numbers.
- Output: the same numbers arranged in sorted (ascending or descending) order.
- Iterative and recursive versions: Sorting algorithms come in iterative versions and recursive versions; for example, insertion sort has an iterative version, which is the one shown.
- Naive (brute-force) approach — permutation method: Generate all permutations of the given numbers, find the one permutation that is in ascending order, and that permutation is the sorted answer.
- Why the brute-force method is rejected: An array of numbers has permutations, so enumerating them takes time of the order of . Since and outgrows every , this is worse than exponential time.
Algorithms of order (exponential) and (super-exponential) are very costly and must be avoided.
How it works
- Take the input list, which is some arbitrary arrangement of numbers.
- Run the sorting algorithm on it.
- After the algorithm completes, the numbers must appear in sorted sequence — for example and maybe , i.e., all elements placed in order.
- The output is either ascending or descending; producing that output is sorting.
The brute-force alternative proceeds as:
- Generate all permutations of the given numbers.
- Among all these permutations, find the one permutation that is in ascending order.
- That permutation is the sorted answer.
Because this costs order , a better method is needed; the next algorithm is insertion sort, whose code is explained with an example.
Formulas
- Number of permutations of distinct elements (n factorial).
- Time to try all permutations , and (a loose upper bound; is super-exponential) — far worse than exponential time.
- = number of possible arrangements of the input numbers.
- = the exponential order that is compared with (both are exponential and too costly).
Worked examples
Input: a list of numbers in arbitrary order, containing the values in some random arrangement.
Required output after sorting (ascending): and maybe — i.e., the same numbers placed in sorted sequence.
Brute-force on this list:
- Step 1: list all permutations of the numbers.
- Step 2: scan the permutations and pick the one that is in ascending order.
- Cost: order of (super-exponential, bounded above by ) → rejected.
Comparison
| Approach | Idea | Time cost | Verdict |
|---|---|---|---|
| Permutation (brute force) | Generate all permutations, pick the ascending one | , exponential | Too costly, avoid |
| Insertion sort (iterative) | Build the sorted order by inserting elements one by one (detailed next) | Better than exponential (to be derived) | Preferred over brute force |
Revision summary
- Sorting = arranging a given sequence of numbers in ascending or descending order.
- Sorting algorithms exist in iterative and recursive versions; the insertion sort shown is the iterative version.
- The brute-force permutation method is correct but takes time (super-exponential, and as a loose upper bound), so it is impractical.
- Exponential algorithms (, ) are very costly and should be avoided; a better algorithm (insertion sort) is needed.
Previous-year exam insights
- A comparison-based sorting algorithm needs at least comparisons in the worst case (decision-tree argument: n! leaves, height ≥ = );
this is the tightest lower bound.
Asked inGATE 2004 - A sorting algorithm is stable if it preserves the relative order of equal (non-distinct) elements; stability is independent of time complexity.
Asked inGATE 1999 - Selection sort performs exactly swaps in the worst case (at most one swap per pass, n−1 passes), while its number of comparisons is .
Asked inGATE 2013GATE 2009 - Heap sort sorts k elements in time; hence the number of elements sortable in time is .
Asked inGATE 2013 - The minimum number of elements to change so that an array becomes sorted equals n minus the length of the longest non-decreasing subsequence (LNDS) of the array.
Asked inGATE 2024 - Sorting by finding the maximum-gradient (steepest) line among n points reduces to sorting the points by x-coordinate (or slope comparisons), giving as the best possible time.
Asked inGATE 2007