Menu

Sorting Algorithms - Code Quiz

1. This code makes a single pass through a list. What is printed?

data = [5, 2, 9, 1] for j in range(0, len(data) - 1): if data[j] > data[j + 1]: temp = data[j] data[j] = data[j + 1] data[j + 1] = temp print(data)
[2, 5, 1, 9]
[5, 2, 9, 1]
[1, 2, 5, 9]
[2, 5, 9, 1]

2. What value is printed?

data = [4, 1, 3, 2] swaps = 0 for j in range(0, len(data) - 1): if data[j] > data[j + 1]: temp = data[j] data[j] = data[j + 1] data[j + 1] = temp swaps = swaps + 1 print(swaps)
4
1
3
2

Log in or sign-up to access the quiz



Don't have a login?

You can still revise this topic