Identify the purpose of the given algorithm :
Step 1: SET i = 0
Step 2: WHILE i< n, REPEAT STEPS 3 to 8
Step 3: SET j = 0
Step 4: WHILE j< n-i-1, REPEAT STEPS 5 to 7
Step 5: IF numList[j] < numList[j+1] THEN
Step 6: swap(numList[j], numList[j+1])
Step 7: SET j=j+1
Step 8: SET i=i+1
Identify the purpose of the given algorithm :
Step 1: SET i = 0
Step 2: WHILE i< n, REPEAT STEPS 3 to 8
Step 3: SET j = 0
Step 4: WHILE j< n-i-1, REPEAT STEPS 5 to 7
Step 5: IF numList[j] < numList[j+1] THEN
Step 6: swap(numList[j], numList[j+1])
Step 7: SET j=j+1
Step 8: SET i=i+1
Solution
The algorithm compares adjacent elements numList[j] and numList[j+1] and swaps them, which is how bubble sort works. The inner loop runs for , so each pass leaves one more element in its final place at the end.
The swap happens when numList[j] < numList[j+1], that is, when the left element is smaller than the right one. Smaller elements move right and larger ones move left, so the list ends up in descending order.
Option 1: bubble sort is correct, but the condition < gives descending order. Ascending order would need >.
Options 2 and 3: selection sort finds the minimum or maximum in the unsorted part and places it at one position. It does not swap adjacent pairs repeatedly.
Related questions:
2026: 11 May Shift 2
2026: 26 May Shift 1
2026: 11 May Shift 2