1. Gyanastra
  2. Algorithms
  3. Elementary Sorting Algorithms
  4. Introduction to Sorting Problem
Free lesson, no account needed 4 min read 14 practice questions after it
Asked in past papers13 times
  • GATE CSE2024202220212013200920072004199919941991
  • ISRO202020172007

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 nn 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 nn numbers has n!n! permutations, so enumerating them takes time of the order of n!n!. Since n!=O(nn)n! = O(n^n) and n!n! outgrows every cnc^n, this is worse than exponential time.
    Algorithms of order 2n2^n (exponential) and nnn^n (super-exponential) are very costly and must be avoided.

How it works

  1. Take the input list, which is some arbitrary arrangement of numbers.
  2. Run the sorting algorithm on it.
  3. After the algorithm completes, the numbers must appear in sorted sequence — for example 1,2,3,5,6,7,8,91, 2, 3, 5, 6, 7, 8, 9 and maybe 00, i.e., all elements placed in order.
  4. The output is either ascending or descending; producing that output is sorting.

The brute-force alternative proceeds as:

  1. Generate all permutations of the given nn numbers.
  2. Among all these permutations, find the one permutation that is in ascending order.
  3. That permutation is the sorted answer.

Because this costs order n!n!, a better method is needed; the next algorithm is insertion sort, whose code is explained with an example.

Formulas

  • Number of permutations of nn distinct elements =n!= n! (n factorial).
  • Time to try all permutations =O(n!)= O(n!), and n!=O(nn)n! = O(n^n) (a loose upper bound; n!n! is super-exponential) — far worse than exponential time.
    • n!n! = number of possible arrangements of the nn input numbers.
    • nnn^n = the exponential order that n!n! is compared with (both are exponential and too costly).

Worked examples

Input: a list of numbers in arbitrary order, containing the values 0,1,2,3,5,6,7,8,90, 1, 2, 3, 5, 6, 7, 8, 9 in some random arrangement.

Required output after sorting (ascending): 1,2,3,5,6,7,8,91, 2, 3, 5, 6, 7, 8, 9 and maybe 00 — i.e., the same numbers placed in sorted sequence.

Brute-force on this list:

  • Step 1: list all n!n! permutations of the nn numbers.
  • Step 2: scan the permutations and pick the one that is in ascending order.
  • Cost: order of n!n! (super-exponential, bounded above by O(nn)O(n^n)) → rejected.

Comparison

ApproachIdeaTime costVerdict
Permutation (brute force)Generate all n!n! permutations, pick the ascending oneO(n!)≈O(nn)O(n!) \approx O(n^n), exponentialToo 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 O(n!)O(n!) time (super-exponential, and O(nn)O(n^n) as a loose upper bound), so it is impractical.
  • Exponential algorithms (O(2n)O(2^n), O(nn)O(n^n)) 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 Ω(nlog⁡n)\Omega(n \log n) comparisons in the worst case (decision-tree argument: n! leaves, height ≥ log⁡2(n!)\log_{2}(n!) = Θ(nlog⁡n)\Theta(n \log n));
    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 Θ(n)\Theta(n) swaps in the worst case (at most one swap per pass, n−1 passes), while its number of comparisons is Θ(n2)\Theta(n^{2}).
    Asked inGATE 2013GATE 2009
  • Heap sort sorts k elements in Θ(klog⁡k)\Theta(k \log k) time; hence the number of elements sortable in Θ(log⁡n)\Theta(\log n) time is Θ(log⁡n/log⁡log⁡n)\Theta(\log n / \log\log n).
    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 Θ(nlog⁡n)\Theta(n \log n) as the best possible time.
    Asked inGATE 2007
Pages 1–2 of 2