Python Bubble Sort Algorithm Implementation

Author: Al-mamun Sarkar Date: 2020-03-29 18:44:45

Python Bubble Sort Algorithm Implementation. The following code shows how to implement a bubble sort algorithm in the Python programming language. 

Code:

class Sort:
    def bubblesort(self, items):
        for n in range(len(items) - 1, 0, -1):
            for i in range(0, n):
                if items[i] > items[i + 1]:
                    items[i + 1], items[i] = items[i], items[i + 1]


items = [190, 20, 310, 450, 60, 110, 121, 270]
sort = Sort()
print('Before Sort')
print(items)
sort.bubblesort(items)
print('After Sort')
print(items)

 

Output:

Before Sort
[190, 20, 310, 450, 60, 110, 121, 270]
After Sort
[20, 60, 110, 121, 190, 270, 310, 450]