Example Bubble Sort

ASnake Documentation

Table Of Contents

    Bubble sort takes a random list and swaps adjacent numbers if they are in the wrong order, until the list is sorted.

    Code:

    import random
    list numbers is 12 : x
    numbers to random.shuffle
    f'original: {numbers}'
    amount is numbers to len
    while amount-- greater than 0 do
        pos is 0
        while pos+1 less than amount
            if numbers[pos+1] > numbers[pos] do
                switch is (numbers[pos],numbers[pos+1])
                numbers[pos] switch[1] 
                numbers[pos+1] switch[0]
            pos++
    f'sorted: {numbers}'
    

    (Potential) Result:

    original: [11, 1, 3, 9, 0, 2, 7, 8, 6, 10, 5, 4]
    sorted: [11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
    
    Scroll to top