chapter

    Sorting Algorithms Notes for Cmi M.sc. and Ph.d. Computer Science

    Sorting Algorithms notes for CMI M.Sc. and Ph.D. Computer Science: 39 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice

    Sorting Algorithms notes

    The sorting problem and records

    Intuition

    The sorting problem and records

    A sorting algorithm outputs a permutation of records such that their keys are in non-decreasing order.

    The problem it solves

    You have a stack of student papers. You want to sort them by grade. Each paper has a name and a grade.

    The analogy of a physical stack stops being valid when records have identical keys. A physical stack has thickness and order, but an array is just a sequence of memory locations where identical keys can be permuted freely unless constrained.

    The idea made exact

    The input is an array of records. Each record has a key and satellite data.

    What the exact version adds. It separates the key from the satellite data. This separation is essential when keys are not unique, because the satellite data must move with its key.

    keys and satellite data

    Defining stability for equal keys

    Derivation

    Defining stability for equal keys

    Assumptions. The input is an array of records. Let have key . A permutation maps the original indices to the output positions.

    array of records permutation
    The steps
    1. Define a valid sort. A permutation is a valid sort if .
    2. Define stability. A sort is stable if for every pair of original indices with , the output places before .
    3. Translate to positions. This means the output positions satisfy .
    4. State the consequence. The relative order of records with equal keys is preserved from the input to the output.
    The stability condition
    Check

    For keys [2, 1, 2, 3], the equal keys are at indices 0 and 2. The only stable sort must place the index 0 record before the index 2 record.

    Distinct keys make stability irrelevant

    Wrong belief

    Swapping means unstable

    A sorting algorithm is unstable if it ever changes the order of elements.

    Why it feels right

    The word stable sounds like it means nothing moves. If elements swap places, it feels unstable.

    Smallest counterexample

    Input keys are [3, 1, 2]. All keys are distinct. Any valid sort must output [1, 2, 3]. The element with key 3 moves from index 0 to index 2. The algorithm changed the order of elements, but it is perfectly stable because there are no equal keys to violate the condition.

    Counter-rule

    Stability only restricts the relative order of records with equal keys. If all keys are distinct, every valid sorting algorithm is stable.

    How it shows up in options

    An option claims an algorithm is unstable because it swaps adjacent elements with different keys. Reject it.

    36 more cards in this chapter

    Free preview ends here

    Login to view the complete notes

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Explore complete test series, official past papers, and chapter-wise study tools.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Sorting Algorithms Notes for CMI M.Sc. and Ph.D. Computer Science

    Sorting Algorithms notes for CMI M.Sc. and Ph.D. Computer Science: 39 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    The sorting problem and records

    Intuition

    The sorting problem and records

    A sorting algorithm outputs a permutation of records such that their keys are in non-decreasing order.

    The problem it solves

    You have a stack of student papers. You want to sort them by grade. Each paper has a name and a grade.

    The analogy of a physical stack stops being valid when records have identical keys. A physical stack has thickness and order, but an array is just a sequence of memory locations where identical keys can be permuted freely unless constrained.

    The idea made exact

    The input is an array of records. Each record has a key and satellite data.

    What the exact version adds. It separates the key from the satellite data. This separation is essential when keys are not unique, because the satellite data must move with its key.

    keys and satellite data

    Defining stability for equal keys

    Derivation

    Defining stability for equal keys

    Assumptions. The input is an array of records. Let have key . A permutation maps the original indices to the output positions.

    array of records permutation
    The steps
    1. Define a valid sort. A permutation is a valid sort if .
    2. Define stability. A sort is stable if for every pair of original indices with , the output places before .
    3. Translate to positions. This means the output positions satisfy .
    4. State the consequence. The relative order of records with equal keys is preserved from the input to the output.
    The stability condition
    Check

    For keys [2, 1, 2, 3], the equal keys are at indices 0 and 2. The only stable sort must place the index 0 record before the index 2 record.

    Distinct keys make stability irrelevant

    Wrong belief

    Swapping means unstable

    A sorting algorithm is unstable if it ever changes the order of elements.

    Why it feels right

    The word stable sounds like it means nothing moves. If elements swap places, it feels unstable.

    Smallest counterexample

    Input keys are [3, 1, 2]. All keys are distinct. Any valid sort must output [1, 2, 3]. The element with key 3 moves from index 0 to index 2. The algorithm changed the order of elements, but it is perfectly stable because there are no equal keys to violate the condition.

    Counter-rule

    Stability only restricts the relative order of records with equal keys. If all keys are distinct, every valid sorting algorithm is stable.

    How it shows up in options

    An option claims an algorithm is unstable because it swaps adjacent elements with different keys. Reject it.

    Stability is a property of the algorithm

    Wrong belief

    Stable output means stable algorithm

    An algorithm is stable if it produces a stable output on a specific input.

    Why it feels right

    We check stability by looking at the output. If the output preserves the order of equal keys, we naturally call the algorithm stable.

    Smallest counterexample

    Consider an algorithm that reverses the array and then runs a stable sort. On input [1, 2, 3], it reverses to [3, 2, 1], then sorts to [1, 2, 3]. The output is stable. But on input [2, 1, 2], it reverses to [2, 1, 2], then sorts to [1, 2, 2]. The original first 2 was at index 0, but the output places the original index 2 record before the index 0 record. The algorithm fails stability on the second input.

    Counter-rule

    Stability is a universal property of the algorithm across all possible inputs, not a property of a single output.

    How it shows up in options

    An option claims an algorithm is stable because its output on a given test case preserves the order of equal keys. Reject it.

    More notes in this unit