FAQ: Bubble Sort: Conceptual - Bubble Sort Review

This community-built FAQ covers the “Bubble Sort Review” exercise from the lesson “Bubble Sort: Conceptual”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Sorting Algorithms

FAQs on the exercise Bubble Sort Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Bubble sort is a good introductory algorithm which opens the door to learning more complex algorithms. It answers the question, “How can we algorithmically sort a list?” and encourages us to ask, “How can we improve this sorting algorithm?”

The last line had me thinking. I’m not sure if this is an improvement. I also rarely use recursion. But this specific case I thought perhaps this could work to lower the runtime. I also don’t know enough about notation to know if what I did was better or worse.

Anyhow, would this be an ideal solution? If so, why? If not, why not?

bubble1 = [1, 3, 5, 7, 9, 0, 2, 4, 6, 8]
bubble2 = [8, 6, 4, 2, 0, 9, 7, 5, 3, 1]
bubble3 = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
bubble4 = [0, 0, 2, 2, 1, 1]
bubble5 = [0, 1, 2, 2, 1, 0]
bubble6 = [0, 1, 2, 0, 1, 2]
bubble7 = [2, 1, 0, 0, 1, 2]


def bubble_sort_ascending(lists):
    x = lists
    b = False

    for i in range(len(x) - 1):
        if x[i] > x[i + 1]:
            b = True
            x[i], x[i + 1] = x[i + 1], x[i]

    return x if b == False else bubble_sort_ascending(x)


print(bubble_sort_ascending(bubble1))
print(bubble_sort_ascending(bubble2))
print(bubble_sort_ascending(bubble3))
print(bubble_sort_ascending(bubble4))
print(bubble_sort_ascending(bubble5))
print(bubble_sort_ascending(bubble6))
print(bubble_sort_ascending(bubble7))

import timeit

Looks like recurssion is slower than a nested loops:

test_code_recurssion="""
def bubble_sort_ascending(lists):
    x = lists
    b = False

    for i in range(len(x) - 1):
        if x[i] > x[i + 1]:
            b = True
            x[i], x[i + 1] = x[i + 1], x[i]

    return x if b == False else bubble_sort_ascending(x)
"""

print("testing time with recurssion: "+str(timeit.timeit(stmt= test_code_recurssion)))

testing time with recurssion: 0.09999450000000001

test_code_nested_loops="""
def bubble_sort_nested_list(lists):
    x = lists
    b = False

    for e in range(len(lists)):
        for i in range(len(x) - 1):
            if x[i] > x[i + 1]:
                x[i], x[i + 1] = x[i + 1], x[i]
    return lists
"""
print("testing time with nested loops: "+str(timeit.timeit(stmt= test_code_nested_loops)))
testing time with nested loops: 0.06398110000000001