Bubble Sort

#!/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]

Comments (0)

  • To add your comment please or

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.

Got It!