The given array is arr = {1,2,3,4,5}. (bubble sort is implemented with a flag…
2025
The given array is arr = {1,2,3,4,5}. (bubble sort is implemented with a flag variable)The number of iterations in selection sort and bubble sort respectively are __________
- A.
5 and 4
- B.
1 and 4
- C.
0 and 4
- D.
4 and 1
Attempted by 290 students.
Show answer & explanation
Correct answer: D
To determine the number of iterations for selection sort and bubble sort on the array {1,2,3,4,5}, consider the behavior of each algorithm.
Selection sort always performs n-1 iterations for an array of size n, regardless of the input order. For n = 5, this means 4 iterations.
Bubble sort with a flag variable checks whether any swaps occurred during an iteration. If no swaps occur, the array is already sorted, and the algorithm terminates early. Since the input array is already sorted, no swaps will occur in the first iteration, and the flag will stop the process after just 1 iteration.
Thus, the number of iterations for selection sort and bubble sort are 4 and 1, respectively.