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)
Top comments (6)
The video is funny, good idea. Numbers are really boring.
aww thanks, took the chance to showcase my favourite people too :)
I want to see quick sort. When will you publish it?
Your wish is my command. Coming up soon :)
Thanks for caring, expecting..., :)
thx :)