Bubble Sort
Algorithm: Bubble Sort
Input: A list of n numbers that are unsorted
Output: The list sorted in ascending order
Steps
- Start
-
Take the list of numbers.
-
Repeat the following steps until the list is completely sorted:
-
Compare each pair of neighbouring elements in the list.
-
If a number is larger than the number next to it, swap their positions.
-
Continue comparing and swapping until the end of the list.
-
-
Keep repeating this process until no more swaps are needed.
-
Display the sorted list.
- Stop
# Bubble sort
def fnBubbleSort(L):
for i in range(0,len(L)):
for j in range(0,len(L)-1):
if(L[j]>L[j+1]):
L[j],L[j+1]=L[j+1],L[j]
L = eval(input("Enter List of elements to be sorted : "))
fnBubbleSort(L)
print(L)
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu