Elementary Sorting: Bubble, Selection and
Insertion – Data Structures and Algorithms:
Exam-Ready Questions and Answers
LATEST UPDATE 2026/2027 | PREMIUM ACADEMIC EDITION
Exam 09 of 40 | Unit code: DSA-09 | Questions: 6 | Total marks: 30 | Answer ALL questions
Question 1: [5 marks]
Explain bubble sort, including the optimization that lets it finish early, and state its
complexities.
Answer:
Bubble sort repeatedly compares adjacent elements and swaps them if they are out of order;
after each pass the largest remaining element is at the end. If a full pass makes no swap, the
array is sorted and the algorithm stops early.
Worst and average case O(n2); best case O(n) with the early-exit flag; space O(1); stable.
Question 2: [4 marks]
Show the result of the first pass of bubble sort on [5, 1, 4, 2, 8].
Answer:
Compare 5 and 1: swap → [1, 5, 4, 2, 8]. Compare 5 and 4: swap → [1, 4, 5, 2, 8]. Compare 5 and
2: swap → [1, 4, 2, 5, 8]. Compare 5 and 8: no swap.
After pass 1: [1, 4, 2, 5, 8].
Question 3: [5 marks]
Describe selection sort and state its complexities.
Answer:
Selection sort repeatedly finds the minimum of the unsorted part and swaps it into the next
position of the sorted part. It makes n(n − 1)/2 comparisons in every case, so its time is always
2
O(n ), with at most n − 1 swaps and O(1) space. The usual version is not stable.
Question 4: [5 marks]
Describe insertion sort. When is it a good choice?
Answer:
Insertion sort builds a sorted prefix: each new element is compared with the sorted elements on
its left and shifted into its correct position. Best case O(n) (already sorted), average and worst
2
case O(n ), space O(1), stable.
It is a good choice for small arrays and for nearly sorted data, and is often used inside hybrid
algorithms.
Page 1 Data Structures and Algorithms • Exam 09
Insertion – Data Structures and Algorithms:
Exam-Ready Questions and Answers
LATEST UPDATE 2026/2027 | PREMIUM ACADEMIC EDITION
Exam 09 of 40 | Unit code: DSA-09 | Questions: 6 | Total marks: 30 | Answer ALL questions
Question 1: [5 marks]
Explain bubble sort, including the optimization that lets it finish early, and state its
complexities.
Answer:
Bubble sort repeatedly compares adjacent elements and swaps them if they are out of order;
after each pass the largest remaining element is at the end. If a full pass makes no swap, the
array is sorted and the algorithm stops early.
Worst and average case O(n2); best case O(n) with the early-exit flag; space O(1); stable.
Question 2: [4 marks]
Show the result of the first pass of bubble sort on [5, 1, 4, 2, 8].
Answer:
Compare 5 and 1: swap → [1, 5, 4, 2, 8]. Compare 5 and 4: swap → [1, 4, 5, 2, 8]. Compare 5 and
2: swap → [1, 4, 2, 5, 8]. Compare 5 and 8: no swap.
After pass 1: [1, 4, 2, 5, 8].
Question 3: [5 marks]
Describe selection sort and state its complexities.
Answer:
Selection sort repeatedly finds the minimum of the unsorted part and swaps it into the next
position of the sorted part. It makes n(n − 1)/2 comparisons in every case, so its time is always
2
O(n ), with at most n − 1 swaps and O(1) space. The usual version is not stable.
Question 4: [5 marks]
Describe insertion sort. When is it a good choice?
Answer:
Insertion sort builds a sorted prefix: each new element is compared with the sorted elements on
its left and shifted into its correct position. Best case O(n) (already sorted), average and worst
2
case O(n ), space O(1), stable.
It is a good choice for small arrays and for nearly sorted data, and is often used inside hybrid
algorithms.
Page 1 Data Structures and Algorithms • Exam 09