Bubble Sort Python Program

  Python program for bubble sort, bubble sort is the simplest way to sort the data. The following program shows the implementation of Bubble Sort algorithms in Python Programming Language.

Bubble Sort Python Program


def bubbleSort(nums):
    for i in range(1, len(nums)-1):  #decrement the loop by 1 each time
        for j in range(len(nums)-i): # Inner loop is used to compare the values
            if nums[j] > nums[j+1]:
                nums[j], nums[j+1] = nums[j+1], nums[j]  #swap and store the value
    return nums

if __name__ == "__main__":
    nums = [55,3,2,99,0,1,5,7,8]
    print('Before Sort:', nums)
    print('After Sort:', nums)


Before Sort: [55, 3, 2, 99, 0, 1, 5, 7, 8]
After Sort: [0, 1, 2, 3, 5, 7, 8, 55, 99]

Leave a Reply

Your email address will not be published. Required fields are marked *