## DEV Community is a community of 605,533 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

loading...

# Bubble Sort in Python

Become a Software Engineer -> Pass Technical Interview -> Be darn good at Data Structure and Algorithms -> Starting with Sorting Algorithms -> 1. Bubble Sort

Bubble Sort (Decreasing Order)

``````def bubblesort(array):
# we minus 1 because we are always comparing the current value with the next value
lengthOfArray = len(array) - 1
# numbe of rounds will be the total length - 1, for array with length 5, we will do 4 rounds: 0 and 1, 1 and 2, 2 and 3, 3 and 4.
for i in range(lengthOfArray):
# at each round, we compare the current j with the next value
for j in range(lengthOfArray - i):
# only swap their positions if left value < right value as we aim to move all the small values to the back
if array[j] < array[j + 1]:
array[j], array[j + 1] = array[j + 1], array[j]

return array

array = [2, 1, 5, 4, 3]
print bubblesort(array)
``````

## Discussion (6) 