DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 963,503 amazing developers

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

Create account Log in
Cover image for Bubble sort algorithm
Aya Bouchiha
Aya Bouchiha

Posted on • Updated on

Bubble sort algorithm

Definition of the bubble sort algorithm

Bubble Sort is a type of sorting algorithms that works by comparing each pair of adjacent items and swapping them if they are in the wrong order.

Alt Text

Space and Time complexity of bubble sort

Time complexity Space complexity
О(n2) O(1)

Bubble sort implementation using python

def BubbleSortAlgorithm(items: list) -> list:
    """
        [name] => Bubble Sort 
        [type] => Sorting algorithms
        [space complexity] => O(1)
        [time complexity]  => O(n^2)
        @params (
            [items] => list
        )
        @return => sorted list
    """
    for i in range(len(items) - 1):
        isSorted = True
        for j in range(len(items) - i - 1):
            # if the number is greater than the adjacent element
            if items[j] > items[j + 1] :
                # swap 
                items[j], items[j + 1] = items[j + 1], items[j]
                isSorted = False
        # if the list is sorted
        if isSorted:
            break
    return items
Enter fullscreen mode Exit fullscreen mode

References and useful resources

#day_8
Have a great day.

Top comments (2)

Collapse
 
aatmaj profile image
Aatmaj

nice!

Collapse
 
ayabouchiha profile image
Aya Bouchiha Author

Thank you have a good day😁

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.