Sorting and Searching Traces: Bubble, Selection, Merge, Quick

Programmer, lecturer and IT officer papers often print a short list and ask what it looks like after one or two passes of a sorting algorithm, or which elements binary search compares before it finds a key. These questions are easy marks if you can run the algorithm by hand without skipping a step. This lesson explains exactly what one pass of each method does, gives a complexity table to memorise, and works through traces that were all checked by running the algorithms in a program.

Asked in: PPSC, FPSC, NTS, ETEA, KPPSC, SSC, IBPS and ICS/ECAT computer

Bubble sort

Bubble sort walks along the list comparing each pair of neighbours and swaps them if the left one is larger. One complete walk is one pass. After the first pass the largest value has bubbled to the last position; after the second pass the second largest is in place, and so on, so each pass can stop one position earlier than the one before.

A list of n items needs at most n − 1 passes. Without any shortcut it makes (n − 1) + (n − 2) + … + 1 = n(n − 1) ÷ 2 comparisons. The total number of swaps equals the number of out-of-order pairs (inversions) in the list. The improved version keeps a flag and stops as soon as a pass makes no swap, so on a list that is already sorted it finishes after one pass.

Selection sort

Selection sort divides the list into a sorted front part and an unsorted back part. In each pass it scans the whole unsorted part for the smallest value and swaps it with the first unsorted position, so after pass k the k smallest values sit at the front in order.

It always makes n(n − 1) ÷ 2 comparisons, whatever the input order, but at most n − 1 swaps, which is useful when writing to memory is costly. The long-distance swap can jump an item over an equal item, so the usual selection sort is not stable.

Insertion sort

Insertion sort works like arranging playing cards in the hand. The first item is treated as a sorted list of one. Each pass takes the next item and shifts the larger sorted items one step right until the correct gap appears, then drops the item into that gap. After pass k, the first k + 1 items are sorted among themselves, though later items may still need to go between them.

On a list that is already sorted, every new item is compared once and stays put, so the best case is O(n). On a list in reverse order every item must travel to the front, giving O(n²). Insertion sort is stable and needs no extra array.

Merge sort

Merge sort is a divide and conquer method. It splits the list into two halves, sorts each half, and then merges the two sorted halves by repeatedly comparing their front items and taking the smaller one. Splitting continues down to single items, which are already sorted, so for 8 items the first round of merging produces four sorted pairs, the next produces two sorted fours and the last gives the sorted eight.

Merging two sorted lists of total length m needs at most m − 1 comparisons; it stops comparing as soon as one list runs out and copies the rest of the other. Merge sort takes O(n log n) time in every case and is stable, but it needs an extra array of size n, so its extra space is O(n).

Quick sort

Quick sort picks one item as the pivot and partitions the list: every item smaller than the pivot goes to its left and every larger item to its right, so the pivot lands in its final sorted position. The left and right parts are then sorted the same way. Textbooks differ in how they pick the pivot and in the exact order of items after a partition, but every scheme puts the same set of smaller items on the left and leaves the pivot at the same final position.

With well-balanced partitions quick sort takes O(n log n) on average, and it is very fast in practice. If the pivot is always the smallest or largest remaining item, for example picking the first or last item of an already sorted list, each partition removes only one item and the time becomes O(n²). The usual quick sort is not stable.

Searching and the complexity table

Linear search checks items one by one, so it works on any list but takes up to n comparisons: O(n). Binary search works only on a sorted list. It compares the key with the middle item; if they match it stops, if the key is smaller it keeps only the left half, otherwise only the right half. With positions numbered from 0, mid = (low + high) ÷ 2 rounded down. Each comparison halves the range, so it needs at most ⌊log₂ n⌋ + 1 comparisons: O(log n). For 15 items that is at most 4.

Complexity table to memorise. Bubble: best O(n) with the early-stop flag, average and worst O(n²), stable. Selection: O(n²) in all cases, not stable. Insertion: best O(n), average and worst O(n²), stable. Merge: O(n log n) in all cases, stable, O(n) extra space. Quick: average O(n log n), worst O(n²), not stable. Linear search O(n); binary search O(log n).

Solved examples

  1. Sort 5, 3, 8, 1 in ascending order with bubble sort and show the list after every pass.
    1. Pass 1: 5 is bigger than 3, swap (3, 5, 8, 1); 5 is smaller than 8, no swap; 8 is bigger than 1, swap. Result 3, 5, 1, 8, and 8 is now fixed.
    2. Pass 2: 3 and 5 are in order, no swap; 5 is bigger than 1, swap. Result 3, 1, 5, 8.
    3. Pass 3: 3 is bigger than 1, swap. Result 1, 3, 5, 8. Three passes were needed for four items.
    Answer: 3, 5, 1, 8 → 3, 1, 5, 8 → 1, 3, 5, 8
  2. Apply selection sort to 64, 25, 12, 22, 11. Show the list after each pass.
    1. Pass 1: the smallest value, 11, is swapped with 64 at the front: 11, 25, 12, 22, 64.
    2. Pass 2: the smallest of 25, 12, 22, 64 is 12, swapped with 25: 11, 12, 25, 22, 64.
    3. Pass 3: the smallest of 25, 22, 64 is 22, swapped with 25: 11, 12, 22, 25, 64.
    4. Pass 4: 25 is already the smaller of the last two, so nothing moves.
    Answer: 11, 25, 12, 22, 64 → 11, 12, 25, 22, 64 → 11, 12, 22, 25, 64
  3. Neha sorts 9, 4, 7, 1 with insertion sort. Give the list after each pass.
    1. Pass 1 inserts 4 into the sorted part (9): 4 is smaller, so 9 shifts right. List 4, 9, 7, 1.
    2. Pass 2 inserts 7 into (4, 9): 9 shifts right, 7 stops after 4. List 4, 7, 9, 1.
    3. Pass 3 inserts 1 into (4, 7, 9): all three shift right and 1 goes to the front. List 1, 4, 7, 9.
    Answer: 4, 9, 7, 1 → 4, 7, 9, 1 → 1, 4, 7, 9
  4. The sorted array 3, 8, 12, 17, 21, 25, 30, 36, 41, 47, 52, 58, 63, 70, 77 is stored at positions 0 to 14. Trace binary search for the key 8, using mid = (low + high) ÷ 2 rounded down.
    1. low = 0, high = 14, mid = 7, item 36. 8 is smaller than 36, so high = 6.
    2. low = 0, high = 6, mid = 3, item 17. 8 is smaller than 17, so high = 2.
    3. low = 0, high = 2, mid = 1, item 8. Found.
    Answer: Compared with 36, 17 and 8: found in 3 comparisons
  5. Show the rounds of merge sort on 6, 1, 7, 3, 8, 2, 5, 4.
    1. The list is split down to eight single items.
    2. Round 1 merges neighbours into sorted pairs: 1, 6 | 3, 7 | 2, 8 | 4, 5.
    3. Round 2 merges pairs into sorted fours: 1, 3, 6, 7 | 2, 4, 5, 8.
    4. Round 3 merges the two fours: 1, 2, 3, 4, 5, 6, 7, 8.
    Answer: 1, 6, 3, 7, 2, 8, 4, 5 → 1, 3, 6, 7, 2, 4, 5, 8 → sorted
  6. Ravi partitions 33, 12, 47, 8, 29, 51, 20 using the last item, 20, as the pivot. Where does the pivot land and what is on each side? Show the result using the common Lomuto scheme.
    1. Items smaller than 20 are 12 and 8, so the pivot must land in position 3 (the third place).
    2. Lomuto scheme: scan left to right and swap every item smaller than the pivot towards the front: 12, 33, 47, 8, … then 12, 8, 47, 33, 29, 51, 20.
    3. Finally swap the pivot into place after the small items: 12, 8, 20, 33, 29, 51, 47. Left part 12, 8; right part 33, 29, 51, 47.
    Answer: Pivot 20 in the third place: 12, 8, 20, 33, 29, 51, 47

Exam shortcuts

Common mistakes

Practice MCQs

  1. Selection sort is applied to 29, 10, 14, 37, 13 to sort it in ascending order. What is the list after the first pass?
    A) 10, 29, 14, 37, 13
    B) 10, 14, 29, 37, 13
    C) 10, 13, 14, 37, 29
    D) 29, 10, 14, 13, 37
    Answer: A) 10, 29, 14, 37, 13 — The first pass finds the smallest value, 10, and swaps it with 29 at the front; nothing else moves. 10, 13, 14, 37, 29 is the list after the second pass, 10, 14, 29, 37, 13 shifts items as insertion sort would, and moving 37 towards the end is how bubble sort behaves.
  2. Which of these sorting algorithms needs extra memory proportional to n in its usual form?
    A) Bubble sort
    B) Merge sort
    C) Insertion sort
    D) Selection sort
    Answer: B) Merge sort — Merge sort copies items into a separate array while merging two halves, so it needs O(n) extra space. Bubble, insertion and selection sort rearrange items inside the original array using only a few spare variables.
  3. What is the best-case time complexity of insertion sort, reached when the list is already sorted?
    A) O(1)
    B) O(log n)
    C) O(n)
    D) O(n²)
    Answer: C) O(n) — On a sorted list each new item is compared once with the item before it and stays put, so the work grows in step with n. O(n²) is its worst case on a reverse-ordered list, and O(1) or O(log n) is impossible because every item must at least be looked at.
  4. Bubble sort is applied to 7, 3, 9, 2, 6 in ascending order. What is the list after the second pass?
    A) 3, 2, 6, 7, 9
    B) 3, 7, 2, 6, 9
    C) 2, 3, 6, 7, 9
    D) 3, 2, 7, 6, 9
    Answer: A) 3, 2, 6, 7, 9 — Pass 1 gives 3, 7, 2, 6, 9 with 9 at the end. Pass 2 compares 3 and 7 (no swap), swaps 7 and 2, then swaps 7 and 6, giving 3, 2, 6, 7, 9. 3, 7, 2, 6, 9 is only the first pass, 2, 3, 6, 7, 9 is the fully sorted list reached after pass 3, and 3, 2, 7, 6, 9 misses the last swap of pass 2.
  5. The sorted array 3, 8, 12, 17, 21, 25, 30, 36, 41, 47, 52, 58, 63, 70, 77 is stored at positions 0 to 14. Binary search with mid = (low + high) ÷ 2 rounded down looks for 58. Which items are compared with the key, in order?
    A) 36, 52, 58
    B) 41, 58
    C) 30, 47, 58
    D) 36, 58
    Answer: D) 36, 58 — The first mid is (0 + 14) ÷ 2 = 7, item 36; 58 is larger, so low becomes 8. The next mid is (8 + 14) ÷ 2 = 11, item 58, found after two comparisons. Starting from 41 or 30 uses the wrong middle position, and 52 is never a mid in this search.
  6. Which of these sorting algorithms is not stable in its usual form?
    A) Merge sort
    B) Selection sort
    C) Insertion sort
    D) Bubble sort
    Answer: B) Selection sort — Selection sort swaps the smallest item into place over a long distance, which can carry an item past another item with the same key and reverse their original order. Merge, insertion and bubble sort only move an item past strictly larger ones, so equal items keep their order.
  7. Insertion sort is applied to 8, 5, 9, 2, 6, where pass 1 inserts the second item, pass 2 the third, and so on. What is the list after pass 3?
    A) 5, 8, 9, 2, 6
    B) 2, 5, 6, 8, 9
    C) 2, 5, 8, 9, 6
    D) 2, 8, 5, 9, 6
    Answer: C) 2, 5, 8, 9, 6 — Pass 1 gives 5, 8, 9, 2, 6; pass 2 leaves 9 where it is; pass 3 inserts 2 at the front, giving 2, 5, 8, 9, 6. 5, 8, 9, 2, 6 is the list after pass 2, the fully sorted list needs one more pass for 6, and 2, 8, 5, 9, 6 is not produced by any step of insertion sort.
  8. Quick sort reaches its worst-case time of O(n²) when:
    A) The pivot is always the smallest or largest remaining item
    B) The pivot is always the median of the items
    C) The list contains only distinct values
    D) The list has an even number of items
    Answer: A) The pivot is always the smallest or largest remaining item — If the pivot is always an extreme value, each partition removes only that one item and leaves the rest in one part, so there are about n levels of partitioning. A median pivot gives the best, balanced split, and distinct values or an even count of items do not decide the running time.
  9. How many comparisons does selection sort make to sort a list of 8 items?
    A) 8
    B) 56
    C) 64
    D) 28
    Answer: D) 28 — Selection sort always scans the whole unsorted part: 7 + 6 + 5 + 4 + 3 + 2 + 1 = 8 × 7 ÷ 2 = 28 comparisons, whatever the input order. 56 forgets to divide by 2, 64 is 8², and 8 is far too few to find the minimum in each pass.
  10. Merge sort is applied to 38, 27, 43, 3, 9, 82, 10, 15. What is the list after the first round of merging, when single items are merged into sorted pairs?
    A) 27, 38, 3, 43, 9, 82, 10, 15
    B) 3, 27, 38, 43, 9, 10, 15, 82
    C) 27, 38, 43, 3, 9, 82, 10, 15
    D) 3, 9, 10, 15, 27, 38, 43, 82
    Answer: A) 27, 38, 3, 43, 9, 82, 10, 15 — The pairs are (38, 27), (43, 3), (9, 82) and (10, 15), and each is put in order: 27, 38 | 3, 43 | 9, 82 | 10, 15. 3, 27, 38, 43, 9, 10, 15, 82 is the second round, the fully sorted list comes after the third, and 27, 38, 43, 3 leaves the pair 43, 3 unsorted.
  11. How many comparisons are made when the sorted lists 2, 5, 9 and 3, 4, 10, 12 are merged into one sorted list?
    A) 7
    B) 6
    C) 4
    D) 5
    Answer: D) 5 — The comparisons are 2 v 3, 5 v 3, 5 v 4, 5 v 10 and 9 v 10; after 9 is taken the first list is empty, so 10 and 12 are copied without comparing. That is 5 comparisons. 6 is the maximum, m − 1, for 7 items, which is not reached here, and 7 counts every item.
  12. Bubble sort with an early-stop flag (it stops after any pass that makes no swap) is applied to 3, 1, 2, 4, 5. How many passes does it make?
    A) 1
    B) 2
    C) 4
    D) 3
    Answer: B) 2 — Pass 1 swaps 3 with 1 and then with 2, giving the sorted list 1, 2, 3, 4, 5. Pass 2 finds no pair out of order, so the flag stops the sort. One pass is not enough because the algorithm cannot know the list is sorted until a pass makes no swap, and 4 passes is what the version without the flag would make.
  13. Using the same 15-item array 3, 8, 12, 17, 21, 25, 30, 36, 41, 47, 52, 58, 63, 70, 77 and the same rule for mid, how many comparisons does binary search make before deciding that 40 is not present?
    A) 3
    B) 5
    C) 4
    D) 15
    Answer: C) 4 — The mids visited are 36 (position 7), 58 (position 11), 47 (position 9) and 41 (position 8); after 41 the range becomes empty, so the search stops after 4 comparisons. This matches the limit ⌊log₂ 15⌋ + 1 = 4, so 5 is impossible, 3 stops one step early, and 15 is what linear search would need.
  14. Quick sort partitions 24, 9, 31, 5, 18, 40, 15 using 15 as the pivot. Which items end up to the left of the pivot?
    A) 9, 5 and 18
    B) 24, 9 and 5
    C) 31 and 40
    D) 9 and 5
    Answer: D) 9 and 5 — Partitioning puts every item smaller than the pivot to its left; only 9 and 5 are smaller than 15, so the pivot lands in the third position. 18 and 24 are larger than 15 and must go to the right, as must 31 and 40.
  15. How many swaps does bubble sort make in total to sort 6, 2, 8, 4, 1 into ascending order?
    A) 10
    B) 4
    C) 7
    D) 6
    Answer: C) 7 — Each bubble sort swap fixes exactly one pair that is in the wrong order, so the total equals the inversions: 6 with 2, 4 and 1; 2 with 1; 8 with 4 and 1; and 4 with 1, which is 7. 10 is the number of comparisons for 5 items, 4 is the number of passes, and 6 misses one inversion.

Questions

Does a bubble sort pass start again from the beginning each time?

Yes. Every pass starts from the first pair and moves right, but it can stop one position earlier than the previous pass because the largest remaining value is already fixed at the end.

Why do different books show different lists after a quick sort partition?

There are several partition schemes, such as Lomuto and Hoare, and they move items in different orders. All of them agree on which items end up left and right of the pivot, so exam questions usually ask about those sets or the pivot’s final position.

Which sorting algorithm is the fastest?

For large random data, quick sort and merge sort, both O(n log n), are much faster than the O(n²) methods. Insertion sort is very quick on small or nearly sorted lists, which is why many libraries use it for small pieces.

Can binary search be used on a linked list?

It works correctly only on sorted data, and it is efficient only when the middle item can be reached directly, as in an array. In a linked list reaching the middle takes O(n) steps, so the speed advantage is lost.