Program to implement bubble sort in Python
In the python programming article, we are going to learn
-
program to implement bubble sort in python
Program to implement bubble sort in Python
The program is as follows:
# Owner : TutorialsInhand Author : Devjeet Roy
def bubble_sort(arr):
for i in range(len(arr)):
for j in range(len(arr)):
if arr[i] < arr[j]:
arr[i],arr[j] = arr[j],arr[i]
return arr
if __name__ == "__main__":
marks = [22,66,43,58,98,42,77,56,66]
result = bubble_sort(marks)
print("The sorted marks: ", result)
The output of the program is as follows:
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
The sorted marks: [22, 42, 43, 56, 58, 66, 66, 77, 98]
Few important tips about the program
1. After each iteration the largest element in the unsorted part finds it correct position.
2. The best time complexity is O(n). The average and worst case time complexities are O(n2)
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :
Published Date :
Oct 13,2022