Bubble Sort Algorithm Implementation

  • Share this:

Code introduction


Sorts an iterable using the bubble sort algorithm.


Technology Stack : List (list), Loop (loop), Tuple (tuple), TypeError handling

Code Type : Function

Code Difficulty :


                
                    
def aca_sort(iterable):
    """
    Sort an iterable using the bubble sort algorithm.
    """
    try:
        iterable = list(iterable)
        n = len(iterable)
        for i in range(n):
            for j in range(0, n-i-1):
                if iterable[j] > iterable[j+1]:
                    iterable[j], iterable[j+1] = iterable[j+1], iterable[j]
        return iterable
    except TypeError:
        return "Input must be a list of comparable elements."