Python Programming

Bubble Sort

Python programming for sorting numbers using bubble sort algorithm

8/14/2021
0 views
bubble-sort.pyPython
#!/usr/bin/evn python

# Function for bubble sort
def bubbleSort(data):
    # Loop the process within range of number list
    for i in range(len(data)-1,0,-1):
        for j in range(i):
            # Compare with next location value
            if data[j]>data[j+1]:
                #Swap numbers as per order
                temp = data[j]
                data[j] = data[j+1]
                data[j+1] = temp

numList = [5,8,1,6,3,7,2,4,9]
print('Before sort:')
print(numList)
# Calling 'bubbleSort' function by passing number array
bubbleSort(numList)
print('After sort:')
print(numList)


# ***** Output *****
# Before sort:
# [5, 8, 1, 6, 3, 7, 2, 4, 9]
# After sort:
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
Pythonpython programmingbubble sortsort numbersorting algorithm

Related Examples