195k views
0 votes
In the bubble sort algorithm, which code accomplishes swapping values in elements at positions index and index + 1? A. list[index] = list[index + 1] list[index + 1] = list[index] B. list[index] = temp; list[index] = list[index + 1]; temp = list[index + 1]; C. temp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp; D. list[index + 1] = list[index] list[index] = list[index + 1]

1 Answer

4 votes

Answer:

Option (C)

Step-by-step explanation:

While swapping two values, we need a temporary variable which needs to store the intermediate value. In bubble sort, in each pass we swap values if smaller element is found at advanced index. So, value at lower index is stored at temp variable and value of higher index is stored at lower one and then finally value at temp is stored at higher index.

Other options are wrong as they did not follow the sequence of swapping.

User Rudker
by
5.1k points