Sorting: Bubble Sort

  • + 0 comments

    Python Solution

        current_num = 0
        swap_num = 0
    
        # Make a number of checks and potential changes equal to the length of the
        # array.
        for i in range(len(a)):
            for j in range(len(a) - 1):
                if a[j] > a[j + 1]:
                    # Store the current number before it changes.
                    current_num = a[j]
    
                    # Perform the swap.
                    a[j] = a[j + 1]
                    a[j + 1] = current_num
    
                    # Count up 1 swap.
                    swap_num += 1
    
        # Print out the statements.
        print(f"Array is sorted in {swap_num} swaps.")
        print(f"First Element: {a[0]}")
        print(f"Last Element: {a[len(a) - 1]}")
        return