In: Computer Science
Bubble Sort is performed on an array with values [6, 1, 2, 0, 5, 4]. What are the array values after two passes of the Bubble Sort? The largest value in the array is moved to the correct location after each pass.
[1, 0, 2, 4, 5, 6] Explanation: ------------- Bubble sort Original list is [6, 1, 2, 0, 5, 4] Iteration: 1 > Swap 6 and 1, since they are not in correct order. Now, the list becomes [1, 6, 2, 0, 5, 4] > Swap 6 and 2, since they are not in correct order. Now, the list becomes [1, 2, 6, 0, 5, 4] > Swap 6 and 0, since they are not in correct order. Now, the list becomes [1, 2, 0, 6, 5, 4] > Swap 6 and 5, since they are not in correct order. Now, the list becomes [1, 2, 0, 5, 6, 4] > Swap 6 and 4, since they are not in correct order. Now, the list becomes [1, 2, 0, 5, 4, 6] > 5 swaps happened in this iteration > List after iteration 1 is [1, 2, 0, 5, 4, 6] Iteration: 2 > Swap 2 and 0, since they are not in correct order. Now, the list becomes [1, 0, 2, 5, 4, 6] > Swap 5 and 4, since they are not in correct order. Now, the list becomes [1, 0, 2, 4, 5, 6] > 2 swaps happened in this iteration > List after iteration 2 is [1, 0, 2, 4, 5, 6] so, list after two passes of bubble sort is [1, 0, 2, 4, 5, 6]