The correct answer is option 3.
Concept:
The given statement follows the bubble sort.
Bubble Sort:
Bubble Sort is the most basic sorting algorithm, which operates by exchanging neighboring elements in the wrong order repeatedly. Bubble Sort is the most basic sorting algorithm, which operates by exchanging neighboring elements in the wrong order repeatedly.
Best Case:
Bubble sort best-case swaps are zero. The list would already be sorted. So no swaps are required.
Best case example:
Input: 1 2 3 4 5
Output: 1 2 3 4 5
Pass 1:
1 2 3 4 5 compare 1, 2 and no swap.
1 2 3 4 5 compare 2, 3 and no swap.
1 2 3 4 5 compare 3, 4 and no swap.
1 2 3 4 5 compare 4, 5 and no swap.
Pass 2:
1 2 3 4 5 compare 1, 2 and no swap.
1 2 3 4 5 compare 2, 3 and no swap.
1 2 3 4 5 compare 3, 4 and no swap.
Pass 3:
1 2 3 4 5 compare 1, 2 and no swap.
1 2 3 4 5 compare 2, 3 and no swap.
Pass 4:
1 2 3 4 5 compare 1, 2 and no swap.
Here statement 1 is true, Best case= zero swaps and n(n-1)/2 comparisons.
Worst Case:
Bubble sort worst case swaps n(n-1)/2. The smallest element is at end of the list so it needs the n(n-1)/2 swaps are required.
Worst-case example:
Input: 5 4 3 2 1
Output: 1 2 3 4 5
Pass 1:
4 5 3 2 1 compare 4, 5 and swap
4 3 5 2 1 compare 3, 5 and swap
4 3 2 5 1 compare 2, 5 and swap
4 3 2 1 5 compare 1, 5 and swap
Pass 2:
3 4 2 1 5 compare 3, 4, and swap
3 2 4 1 5 compare 2, 4 and swap
3 2 1 4 5 compare 1, 4 and swap
Pass 3:
2 3 1 4 5 compare 2, 3, and swap
2 1 3 4 5 compare 1, 3, and swap
Pass 4:
1 2 3 4 5 compare 1, 2 and swap
Total swaps are 1+2+3+4 =10 =5(5-1)/2
Here statement 2 is true, Worst case = n(n-1)/2 swaps and n(n-1)/2 comparsions.
Important Points
Statement 3 and statement 4 is true.
- In the first pass of the bubble sorts the highest element at the end. After each pass, the next highest element is stored.
- If there are no swaps in the list then the list is stored in order but the bubble sort runs n-1 passes.
Hence the correct answer is Bubble sort.