chapter
    Elementary Sorting Algorithms Notes for GATE DA

    Elementary Sorting Algorithms notes for GATE DA: 22 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    elementary sorting algorithms notes

    Chapter Roadmap: Elementary Sorting Algorithms

    Chapter Roadmap

    Elementary Sorting Algorithms

    Topic 1: Bubble, Insertion and Selection Sort Pass Analysis
    Master the invariants, intermediate states, and exact operation counts for the three elementary sorts. (Current Topic)
    Topic 2: Insertion Sort Swaps and Nearly Sorted Arrays
    Deep dive into shifts, swaps, and the mathematics of inversions in partially sorted data.

    The Core Idea of Pass Analysis

    The Core Idea of Pass Analysis

    The Concept of a "Pass"

    A pass is one complete iteration of the algorithm's primary loop. In exam questions, you are rarely asked to sort the whole array. You are asked to determine the state of the array after exactly passes.

    The Golden Rule

    Do not trace the whole algorithm. Identify the invariant: what is mathematically guaranteed to be in its final sorted position after pass ?

    • Bubble Sort: Locks elements at the end.
    • Selection Sort: Locks elements at the beginning.
    • Insertion Sort: Sorts a prefix of the array.

    Bubble Sort: Mechanics and Invariants

    Bubble Sort: Mechanics & Invariants

    Repeatedly step through the list, compare adjacent elements, and swap them if they are in the wrong order.

    The Pass Invariant: After pass , the largest elements are in their final sorted positions at the end of the array.

    Comparison Count

    In the standard implementation (without early termination), the number of comparisons in pass is strictly deterministic:

    • Pass 1: comparisons
    • Pass 2: comparisons
    • Pass : comparisons
    Total comparisons for passes =

    19 more cards in this chapter

    Try a question

    Answer it here to see how it works. Nothing is recorded until you sign in.

    Question 1
    Level 1: Warm-up

    Consider the array . It is given that Insertion Sort takes exactly 3 swaps to sort . Which of the following statements must be true about ?

    Question 2
    Level 1: Warm-up
    Consider the following assertion and reason: Assertion (A): After exactly 2 passes of Bubble Sort on an array of 8 elements, the 2 largest elements are in their final sorted positions at the end of the array. Reason (R): In Bubble Sort, each pass guarantees that the largest unsorted element moves to its correct position at the end.
    Question 3
    Level 1: Warm-up
    Consider the following assertion and reason: Assertion (A): After exactly 3 passes of Selection Sort on an array of 10 elements, the 3 smallest elements are in their final sorted positions at the beginning of the array. Reason (R): In each pass, Selection Sort finds the maximum element in the unsorted portion and swaps it to the end of the array.
    Question 4
    Level 1: Warm-up
    Consider the following assertion and reason: Assertion (A): After exactly 3 passes of Insertion Sort on an array of 10 elements, the first 4 elements are sorted relative to each other. Reason (R): In each pass, the algorithm compares adjacent elements and swaps them if they are in the wrong order, causing the largest unsorted element to bubble to the end.
    Question 5
    Level 1: Warm-up
    Consider the following assertion and reason: Assertion (A): After exactly 3 passes of Insertion Sort, the first 4 elements of the array are guaranteed to be sorted relative to each other. Reason (R): Insertion Sort achieves this by finding the minimum element in the unsorted suffix and swapping it with the first unsorted element.
    Question 6
    Level 1: Warm-up

    Which of the following statements about Selection Sort is ALWAYS true, regardless of the initial order of elements?

    Question 7
    Level 1: Warm-up

    Consider the standard implementation of Selection Sort (which always performs a swap, even if the minimum is already in place). Which of the following statements about the number of swaps is true?

    Question 8
    Level 1: Warm-up

    Which of the following correctly describes the exact total number of comparisons and total number of swaps in standard Selection Sort for an array of size ?

    Question 9
    Level 1: Warm-up

    Which of the following correctly describes the number of comparisons performed in the -th pass of Selection Sort on an array of elements?

    Question 10
    Level 1: Warm-up

    Suppose Insertion Sort is applied to an array of 5 distinct elements. Let be the total number of inversions and be the total number of comparisons. Which of the following combinations of and is impossible?

    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.

    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.

    Elementary Sorting Algorithms Notes for GATE DA

    Elementary Sorting Algorithms notes for GATE DA: 22 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    Chapter Roadmap: Elementary Sorting Algorithms

    Chapter Roadmap

    Elementary Sorting Algorithms

    Topic 1: Bubble, Insertion and Selection Sort Pass Analysis
    Master the invariants, intermediate states, and exact operation counts for the three elementary sorts. (Current Topic)
    Topic 2: Insertion Sort Swaps and Nearly Sorted Arrays
    Deep dive into shifts, swaps, and the mathematics of inversions in partially sorted data.

    The Core Idea of Pass Analysis

    The Core Idea of Pass Analysis

    The Concept of a "Pass"

    A pass is one complete iteration of the algorithm's primary loop. In exam questions, you are rarely asked to sort the whole array. You are asked to determine the state of the array after exactly passes.

    The Golden Rule

    Do not trace the whole algorithm. Identify the invariant: what is mathematically guaranteed to be in its final sorted position after pass ?

    • Bubble Sort: Locks elements at the end.
    • Selection Sort: Locks elements at the beginning.
    • Insertion Sort: Sorts a prefix of the array.

    Bubble Sort: Mechanics and Invariants

    Bubble Sort: Mechanics & Invariants

    Repeatedly step through the list, compare adjacent elements, and swap them if they are in the wrong order.

    The Pass Invariant: After pass , the largest elements are in their final sorted positions at the end of the array.

    Comparison Count

    In the standard implementation (without early termination), the number of comparisons in pass is strictly deterministic:

    • Pass 1: comparisons
    • Pass 2: comparisons
    • Pass : comparisons
    Total comparisons for passes =

    Insertion Sort: Mechanics and Invariants

    Insertion Sort: Mechanics & Invariants

    Divide the array into a "sorted" and "unsorted" region. Repeatedly take the first element of the unsorted region and insert it into the correct position in the sorted region by shifting larger elements to the right.

    The Pass Invariant: After pass , the first elements of the array are sorted relative to each other.
    (Note: Pass 1 processes the 2nd element, so after pass 1, the first 2 elements are sorted).

    Comparison Count

    The number of comparisons is data-dependent.

    • In pass , the algorithm compares the key against elements in the sorted prefix until it finds the correct spot.
    • Comparisons range from (best case, already in place) to (worst case, needs to go to the front).

    Elementary Sorting Algorithms: Solved Questions with Step-by-Step Explanations (10 Problems)

    Question 1 · Programming, Data Structures and Algorithms MCQ

    Consider the array . It is given that Insertion Sort takes exactly 3 swaps to sort . Which of the following statements must be true about ?

    1. A.

      must be strictly less than 3

    2. B.

      can be greater than or equal to 9

    3. C.

      must be between 3 and 7

    4. D.

      cannot be 10

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a bounding question. We use the inversion sum to establish strict bounds on the unknown element .

    Step 1: Split the array into Left, , and Right.

    • Left has 0 inversions.
    • Right has 1 inversion (since ).

    Step 2: Use the total inversion formula. "Swaps" means shifts, which equals inversions.

    Total =

    Step 3: Analyze the cases for against the Right part .

    • Case 1: . Then (both 9 and 7 are ). This forces . This is consistent because means is greater than all elements in . So is a valid solution.
    • Case 2: . Then (only 7 is ). This forces . This means exactly 1 element in is , so . This contradicts .
    • Case 3: . Then . This forces . This means exactly 2 elements in are , so . This is consistent with .

    Step 4: Combine valid ranges: OR .

    Answer: B

    Common trap: Ignoring the inversions within the Right part, which leads to the incorrect conclusion that must be strictly less than 3.

    Question 2 · Programming, Data Structures and Algorithms MCQ
    Consider the following assertion and reason: Assertion (A): After exactly 2 passes of Bubble Sort on an array of 8 elements, the 2 largest elements are in their final sorted positions at the end of the array. Reason (R): In Bubble Sort, each pass guarantees that the largest unsorted element moves to its correct position at the end.
    1. A.

      Both A and R are true, and R is the correct explanation of A

    2. B.

      Both A and R are true, but R is not the correct explanation of A

    3. C.

      A is true, but R is false

    4. D.

      A is false, but R is true

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is an assertion-reason question testing Bubble Sort pass invariant.

    Step 1: Evaluate Assertion (A):

    • Bubble Sort pass invariant: after k passes, the k largest elements are at the end
    • After 2 passes, the 2 largest elements are at the end
    • Assertion A is TRUE

    Step 2: Evaluate Reason (R):

    • In each pass, Bubble Sort compares adjacent elements and swaps if needed
    • The largest unsorted element "bubbles up" to the end
    • Reason R is TRUE

    Step 3: Check if R explains A:

    • R states that each pass moves the largest unsorted element to the end
    • After pass 1: largest element is at the end
    • After pass 2: second largest element is at position n-1 (since largest is already at n)
    • So after 2 passes, the 2 largest are at the end
    • R correctly explains why A is true

    Answer: A

    Question 3 · Programming, Data Structures and Algorithms MCQ
    Consider the following assertion and reason: Assertion (A): After exactly 3 passes of Selection Sort on an array of 10 elements, the 3 smallest elements are in their final sorted positions at the beginning of the array. Reason (R): In each pass, Selection Sort finds the maximum element in the unsorted portion and swaps it to the end of the array.
    1. A.

      Both A and R are true, and R is the correct explanation of A

    2. B.

      Both A and R are true, but R is not the correct explanation of A

    3. C.

      A is true, but R is false

    4. D.

      A is false, but R is true

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a construction question evaluating the pass invariants and mechanics of Selection Sort.

    Step 1: Evaluate Assertion (A). Selection Sort's pass invariant states that after passes, the smallest elements are in their final sorted positions at the beginning of the array. For , the 3 smallest elements are at the beginning. Assertion A is TRUE.

    Step 2: Evaluate Reason (R). Selection Sort works by finding the MINIMUM element in the unsorted portion and swapping it to the BEGINNING of the unsorted portion. Reason R describes finding the MAXIMUM and swapping to the END, which is the mechanics of Bubble Sort (or a variant of Selection Sort that sorts from the end, but standard Selection Sort finds the min). Reason R is FALSE.

    Step 3: Since A is true and R is false, the correct option is C.

    Answer: C

    Question 4 · Programming, Data Structures and Algorithms MCQ
    Consider the following assertion and reason: Assertion (A): After exactly 3 passes of Insertion Sort on an array of 10 elements, the first 4 elements are sorted relative to each other. Reason (R): In each pass, the algorithm compares adjacent elements and swaps them if they are in the wrong order, causing the largest unsorted element to bubble to the end.
    1. A.

      Both A and R are true, and R is the correct explanation of A

    2. B.

      A is true, but R is false

    3. C.

      A is false, but R is true

    4. D.

      Both A and R are true, but R is not the correct explanation of A

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a construction question evaluating the pass invariants and mechanics of different sorting algorithms.

    Step 1: Evaluate Assertion (A). Insertion Sort's pass invariant states that after pass , the first elements are sorted relative to each other. For , the first elements are sorted. Assertion A is TRUE.

    Step 2: Evaluate Reason (R). The reason describes comparing adjacent elements and swapping to bubble the largest to the end. This is the exact mechanics of Bubble Sort, not Insertion Sort. Insertion Sort takes a key and inserts it into the sorted prefix by shifting elements. Reason R is FALSE.

    Step 3: Since A is true and R is false, the correct option is B.

    Answer: B

    Question 5 · Programming, Data Structures and Algorithms MCQ
    Consider the following assertion and reason: Assertion (A): After exactly 3 passes of Insertion Sort, the first 4 elements of the array are guaranteed to be sorted relative to each other. Reason (R): Insertion Sort achieves this by finding the minimum element in the unsorted suffix and swapping it with the first unsorted element.
    1. A.

      A is true, but R is false

    2. B.

      Both A and R are true, and R is the correct explanation of A

    3. C.

      A is false, but R is true

    4. D.

      Both A and R are true, but R is not the correct explanation of A

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is a construction question evaluating the pass invariants and mechanics of different sorting algorithms.

    Step 1: Evaluate Assertion (A). Insertion Sort's pass invariant states that after pass , the first elements are sorted. For , the first 4 elements are sorted. Assertion A is TRUE.

    Step 2: Evaluate Reason (R). The reason describes finding the minimum in the unsorted suffix and swapping it to the front. This is the exact mechanics of Selection Sort, not Insertion Sort. Reason R is FALSE.

    Step 3: Since A is true and R is false, the correct option is A.

    Answer: A

    Question 6 · Programming, Data Structures and Algorithms MCQ

    Which of the following statements about Selection Sort is ALWAYS true, regardless of the initial order of elements?

    1. A.

      The number of comparisons depends on the initial arrangement of elements

    2. B.

      After k passes, the k largest elements are in their final positions at the end of the array

    3. C.

      The total number of comparisons for a full sort is always n(n - 1) / 2

    4. D.

      The algorithm terminates early if no swaps occur in a pass

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a statement evaluation question about Selection Sort invariants.

    Step 1: Recall that Selection Sort always scans the entire unsorted portion to find the minimum, regardless of data order.

    Step 2: Evaluate each option:

    • Option A: FALSE. Selection Sort comparisons are invariant to data.
    • Option B: FALSE. Selection Sort locks the k smallest at the beginning, not k largest at the end (that's Bubble Sort).
    • Option C: TRUE. Total comparisons = (n-1) + (n-2) + ... + 1 = n(n-1)/2, always.
    • Option D: FALSE. Standard Selection Sort doesn't have early termination.

    Step 3: The correct answer is C.

    Answer: C

    Question 7 · Programming, Data Structures and Algorithms MCQ

    Consider the standard implementation of Selection Sort (which always performs a swap, even if the minimum is already in place). Which of the following statements about the number of swaps is true?

    1. A.

      The total number of swaps depends on the initial order of the array.

    2. B.

      The algorithm performs exactly 1 swap per pass, resulting in swaps for an array of size .

    3. C.

      The algorithm performs 0 swaps if the array is already sorted.

    4. D.

      The number of swaps per pass varies between 0 and .

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a bounding question evaluating the invariant properties of Selection Sort's swap count.

    Step 1: Recall the mechanics of standard Selection Sort. In each pass, it finds the minimum element in the unsorted portion and swaps it with the first element of the unsorted portion.

    Step 2: The problem specifies the "standard implementation" which "always performs a swap, even if the minimum is already in place".

    Step 3: This means every pass executes exactly 1 swap operation.

    Step 4: Since there are passes (to sort elements), the total number of swaps is exactly , regardless of the input data.

    Answer: B

    Question 8 · Programming, Data Structures and Algorithms MCQ

    Which of the following correctly describes the exact total number of comparisons and total number of swaps in standard Selection Sort for an array of size ?

    1. A.

      Comparisons: depends on input, Swaps:

    2. B.

      Comparisons: , Swaps: depends on input

    3. C.

      Comparisons: , Swaps:

    4. D.

      Comparisons: , Swaps:

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a bounding question evaluating the invariant properties of Selection Sort's operation counts.

    Step 1: Recall the mechanics of standard Selection Sort. In each pass , it scans the entire unsorted suffix to find the minimum, requiring exactly comparisons.

    Step 2: Summing comparisons over all passes gives . This is fixed and independent of the input data.

    Step 3: In each pass, it performs exactly 1 swap (even if swapping an element with itself). Over passes, this results in exactly swaps.

    Step 4: Both counts are strictly deterministic for standard Selection Sort.

    Answer: C

    Question 9 · Programming, Data Structures and Algorithms MCQ

    Which of the following correctly describes the number of comparisons performed in the -th pass of Selection Sort on an array of elements?

    1. A.

      It depends on the initial order of elements, ranging from to .

    2. B.

      It is exactly , regardless of the initial order of elements.

    3. C.

      It is exactly , regardless of the initial order of elements.

    4. D.

      It is exactly , regardless of the initial order of elements.

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a bounding question evaluating the invariant properties of Selection Sort's comparison count.

    Step 1: Recall the mechanics of Selection Sort. In each pass , it scans the entire unsorted suffix to find the minimum.

    Step 2: The unsorted suffix has size . Finding the minimum requires exactly comparisons.

    Step 3: This count is strictly deterministic and does not depend on the initial order of the elements.

    Answer: C

    Question 10 · Programming, Data Structures and Algorithms MCQ

    Suppose Insertion Sort is applied to an array of 5 distinct elements. Let be the total number of inversions and be the total number of comparisons. Which of the following combinations of and is impossible?

    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    D

    Step-by-Step Solution

    Key idea: For Insertion Sort, , where . This implies .

    Step 1: For , the formula is , where .

    Step 2: This means .

    Step 3: Check each option:

    • A: . . Possible (already sorted array).
    • B: . . Possible (reverse sorted array).
    • C: . . Possible.
    • D: . is false. Impossible.

    Answer: D

    Common trap: Students may overcount the number of elements that can hit the boundary, or forget that .

    More notes in this unit