Python Program to Bubble Sort
Example:
nums = []
print("Enter Size of List: ")
listSize = int(input())
print("Enter any " + str(listSize) + " Numbers: ")
for i in range(listSize):
nums.insert(i, int(input()))
for i in range(listSize-1):
for j in range(listSize-i-1):
if nums[j]>nums[j+1]:
temp = nums[j]
nums[j] = nums[j+1]
nums[j+1] = temp
print("Sorted List: ")
for i in range(listSize):
print(nums[i])
Output:
Enter Size of List: 5 50 40 30 10 20 Enter any 5 Numbers: Sorted List: 10 20 30 40 50
Share this page on:
Subscribe Email Updates
to get latest update