def bubble_up(values: list, left: int, right: int) -> None: """Bubble up through values[left: right+1], swapping items that are out of order. Note that use of this slicing notation means that items values[left], values[left + 1], values[left + 2], ..., values[right] could be modified. Precondition: left and right are valid indexes in values. >>> list_example_1 = [4, 3, 2, 1, 0] >>> bubble_up(list_example_1, 0, 3) >>> list_example_1 [3, 2, 1, 4, 0] >>> list_example_2 = [4, 3, 2, 1, 0] >>> bubble_up(list_example_2, 2, 4) >>> list_example_2 [4, 3, 1, 0, 2] """ for i in range(left, right): if values[i] > values[i + 1]: temp = values[i] values[i] = values[i + 1] values[i + 1] = temp # Use the bubble_up function as a starting point to help you complete the # bubble_down function. def bubble_down(values: list, left: int, right: int) -> None: """Bubble down through values[left: right+1], swapping items that are out of order. Note that use of this slicing notation means that items values[left], values[left + 1], values[left + 2], ..., values[right] could be modified. Precondition: left and right are valid indexes in values. >>> list_example_1 = [4, 3, 2, 1, 0] >>> bubble_down(list_example_1, 1, 3) >>> list_example_1 [4, 1, 3, 2, 0] >>> list_example_2 = [4, 3, 2, 1, 0] >>> bubble_down(list_example_2, 0, 4) >>> list_example_2 [0, 4, 3, 2, 1] """
def bubble_up(values: list, left: int, right: int) -> None:
"""Bubble up through values[left: right+1], swapping items that are out of
order. Note that use of this slicing notation means that items
values[left], values[left + 1], values[left + 2], ..., values[right] could
be modified.
Precondition: left and right are valid indexes in values.
>>> list_example_1 = [4, 3, 2, 1, 0]
>>> bubble_up(list_example_1, 0, 3)
>>> list_example_1
[3, 2, 1, 4, 0]
>>> list_example_2 = [4, 3, 2, 1, 0]
>>> bubble_up(list_example_2, 2, 4)
>>> list_example_2
[4, 3, 1, 0, 2]
"""
for i in range(left, right):
if values[i] > values[i + 1]:
temp = values[i]
values[i] = values[i + 1]
values[i + 1] = temp
# Use the bubble_up function as a starting point to help you complete the
# bubble_down function.
def bubble_down(values: list, left: int, right: int) -> None:
"""Bubble down through values[left: right+1], swapping items that are out
of order. Note that use of this slicing notation means that items
values[left], values[left + 1], values[left + 2], ..., values[right] could
be modified.
Precondition: left and right are valid indexes in values.
>>> list_example_1 = [4, 3, 2, 1, 0]
>>> bubble_down(list_example_1, 1, 3)
>>> list_example_1
[4, 1, 3, 2, 0]
>>> list_example_2 = [4, 3, 2, 1, 0]
>>> bubble_down(list_example_2, 0, 4)
>>> list_example_2
[0, 4, 3, 2, 1]
"""
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 4 images