LearnFrenzy
Sign in Take a free test

DSA & Algorithms · Topic 7 of 13

Sorting and Searching: questions with answers

Bubble, selection, insertion, merge, quick, heap, counting and radix sort traced pass by pass, stability and in-place sorting, binary search and its variants, and library sorts.

Questions
45
Timed sets
3
Answers run or computed
29
Your accuracy
–

What this topic covers

18 subtopics
  • Binary search7
  • Library sorts6
  • Binary search variants4
  • Merge sort4
  • Insertion sort3
  • Quick sort3
  • Stability3
  • Binary search on the answer2
  • Bubble sort2
  • Non-comparison sorts2
  • Selection2
  • External sorting1
Show all 18 subtopics
  • Heap sort1
  • Lower bounds1
  • Searching1
  • Selection sort1
  • Shell sort1
  • Space1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

Sorting questions in GATE and placement tests are mostly traces: what does the array look like after one pass, or after partitioning? Learn what each algorithm guarantees after k passes – bubble sort fixes the largest k elements at the end, selection sort the smallest k at the front, insertion sort keeps the first k + 1 sorted. Binary search questions test the boundaries: lo <= hi or lo < hi, mid + 1 or mid. All traces here were computed by running the algorithm.

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 Bubble sort Beginner Practice Answer computed

What does the array look like after the first pass of bubble sort (ascending)?

Array
a
  1. 05
  2. 11
  3. 24
  4. 32
  5. 48
  1. 1
  2. 4
  3. 2
  4. 5
  5. 8
  1. 1
  2. 5
  3. 4
  4. 2
  5. 8
  1. 1
  2. 2
  3. 4
  4. 5
  5. 8
  1. 5
  2. 1
  3. 4
  4. 2
  5. 8
Show answer and explanation
Correct answer A

Explanation

One pass compares neighbours left to right and swaps when out of order: (5,1) swap, (5,4) swap, (5,2) swap, (5,8) no swap. The largest element "bubbles" to the end.

Why the other options are wrong

  • BOnly the first swap is applied.
  • CThat is fully sorted – it needs another pass.
  • DThe pass makes three swaps.
Question 2 Binary search Beginner Practice Answer computed

Binary search for 23 uses mid = (lo + hi) // 2. Which values are compared, in order?

Array
a
  1. 02lo
  2. 15
  3. 28
  4. 312
  5. 416
  6. 523
  7. 638
  8. 756
  9. 872
  10. 991hi
23
165623
163823
1623
Show answer and explanation
Correct answer B

Explanation

lo = 0, hi = 9 → mid 4 (16 < 23) → lo = 5 → mid 7 (56 > 23) → hi = 6 → mid 5 (23) found. Three comparisons for ten elements.

Why the other options are wrong

  • AThe first middle is index 4.
  • CThe middle of 5–9 is index 7.
  • DAfter 16 the range is indices 5–9, whose middle is 56.
Question 3 Quick sort Intermediate Practice Answer computed

Quick sort partitions with the last element (70) as pivot using the Lomuto scheme. What is the array after this single partition?

Array
a
  1. 010
  2. 180
  3. 230
  4. 390
  5. 440
  6. 550
  7. 670
  1. 10
  2. 30
  3. 40
  4. 50
  5. 70
  6. 80
  7. 90
  1. 30
  2. 10
  3. 40
  4. 50
  5. 70
  6. 80
  7. 90
  1. 10
  2. 80
  3. 30
  4. 90
  5. 40
  6. 50
  7. 70
  1. 10
  2. 30
  3. 40
  4. 50
  5. 70
  6. 90
  7. 80
Show answer and explanation
Correct answer D

Explanation

Lomuto keeps a boundary i of elements ≤ pivot: 10, 30, 40, 50 are swapped forward in turn, then the pivot is swapped into position 4. Elements on each side are not sorted yet – 90, 80 remain in that order.

Why the other options are wrong

  • APartitioning does not sort the right side.
  • BLomuto keeps the small elements in their scan order.
  • CElements are moved.
Question 4 Merge sort Advanced Practice Answer computed

How many inversions (pairs i < j with a[i] > a[j]) does this array have? (A modified merge sort counts them in O(n log n).)

Array
a
  1. 01
  2. 120
  3. 26
  4. 34
  5. 45

The answer is a number – work it out, then open the solution.

Show answer and explanation
Correct answer 5

Explanation

(20, 6), (20, 4), (20, 5), (6, 4), (6, 5). During a merge, when an element from the right half is taken first, it is smaller than every element still waiting in the left half – add that count in one step.

41 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

DSA & Algorithms

All 13 topics

Topics, levels and full mocks
  1. 01 Time and Space Complexity45 questions · 3 levels
  2. 02 Arrays and Strings45 questions · 3 levels
  3. 03 Linked Lists45 questions · 3 levels
  4. 04 Stacks and Queues45 questions · 3 levels
  5. 05 Hashing45 questions · 3 levels
  6. 06 Recursion and Backtracking45 questions · 3 levels
  7. 07 Sorting and Searching45 questions · 3 levels
  8. 08 Trees and BST45 questions · 3 levels
  9. 09 Heaps and Priority Queues45 questions · 3 levels
  10. 10 Graphs45 questions · 3 levels
  11. 11 Dynamic Programming45 questions · 3 levels
  12. 12 Greedy and Bit Manipulation45 questions · 3 levels
  13. 13 GATE-Style DSA & Algorithms Questions (MSQ & NAT)45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links