0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 7
click for more info
No active XP Potion
Accept a Quest
Login to submit answers
Wait, if the standard sorted()
function exists, why should we learn to write a sorting algorithm from scratch?
In all seriousness, in this chapter we'll be building some of the most famous sorting algorithms from scratch because:
Bubble sort is a very basic sorting algorithm named for the way elements "bubble up" to the top of the list.
Bubble sort repeatedly steps through a slice and compares adjacent elements, swapping them if they are out of order. It continues to loop over the slice until the whole list is completely sorted. Here's the pseudocode:
swapping
to True
end
to the length of the input listswapping
is True
:
swapping
to False
i
from the 2nd element to end
:
(i-1)
th element of the input list is greater than the i
th element:
(i-1)
th element and the i
th elementswapping
to True
end
by oneWhile our avocado toast influencers were happy with our search functionality, now they want to be able to sort all their followers by follower count. Bubble sort is a straightforward sorting algorithm that we can implement quickly, so let's do that!
Complete the bubble_sort
function according to the described algorithm above.
Focus Editor
Alt+Shift+]
Next Tab
Alt+Shift+[
Become a member to Submit
Become a member to Run
Become a member to view solution