Bubble sort


Bubble Sort


Algorithm: Bubble Sort

Input: A list of n numbers that are unsorted

Output: The list sorted in ascending order


Steps

  1.  Start
  2. Take the list of numbers.

  3. Repeat the following steps until the list is completely sorted:

    1. Compare each pair of neighbouring elements in the list.

    2. If a number is larger than the number next to it, swap their positions.

    3. Continue comparing and swapping until the end of the list.

  4. Keep repeating this process until no more swaps are needed.

  5. Display the sorted list.

  6. 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