PRACTICE QUESTIONS WITH ANSWERS
Read the questions carefully and study the answers to confirm your understanding.
Q1. Write the pseudocode for Bubble Sort that sorts a list of 5 numbers in ascending order.
Answer:
DECLARE List[5] : INTEGER
// Assume List already contains 5 numbers
FOR X ← 1 TO 4
FOR Y ← 1 TO 5 – X
IF List[Y] > List[Y + 1] THEN
// Swap values
Temp ← List[Y]
List[Y] ← List[Y + 1]
List[Y + 1] ← Temp
ENDIF
NEXT Y
NEXT X
This algorithm compares each pair of adjacent values and swaps them if they are in the wrong order.
Q2. The values in the list are: 9, 4, 6, 1. Show the result after the first pass of Bubble Sort.
Answer:
List: 9, 4, 6, 1
Pass 1 comparisons:
Compare 9 and 4 → swap → 4, 9, 6, 1
Compare 9 and 6 → swap → 4, 6, 9, 1
Compare 9 and 1 → swap → 4, 6, 1, 9
After Pass 1: 4, 6, 1, 9
Q3. Explain why Bubble Sort is called a “Bubble” Sort.
Answer:
Bubble Sort is named because the largest values ‘bubble’ up to the end of the list during each pass, similar to how air bubbles rise to the surface of water. Each pass pushes the biggest remaining value to its correct position at the end.
Q4. What change in the condition below will sort the list in descending order instead of ascending?
Current condition:
IF List[Y] > List[Y + 1] THEN
Answer:
To sort in descending order, we reverse the comparison:
IF List[Y] < List[Y + 1] THEN
Now the largest values will move to the front of the list.
Q5. Write the output after Bubble Sorting the following list in ascending order:
List = (5, 3, 8, 2)**
Answer:
Pass 1:
5 & 3 → swap → 3, 5, 8, 2
5 & 8 → no swap
8 & 2 → swap → 3, 5, 2, 8
Pass 2:
3 & 5 → no swap
5 & 2 → swap → 3, 2, 5, 8
Pass 3:
3 & 2 → swap → 2, 3, 5, 8
Final Sorted List:
2, 3, 5, 8
