Computer Science, asked by kaushik25112004, 6 months ago

Write a program to read ‘n‘ integer elements, store them in

List, sort the elements in ascending order using Bubblesort

technique also print the sorted list.​

Answers

Answered by Oreki
1

Code:

def bubble_sort(lst):

   for i in range(len(lst) - 1):

       for j in range(0, len(lst) - i - 1):

           if lst[j] > lst[j + 1]:

               lst[j], lst[j + 1] = lst[j + 1], lst[j]

lst = [int(num) for num in input("Enter the elements - ").split()]

print("List Before -", lst)

bubble_sort(lst)

print("List After  -", lst)

Sample I/O:

Enter the elements - 1 2 3 4 51 3 54 32 33 5 4  

List Before - [1, 2, 3, 4, 51, 3, 54, 32, 33, 5, 4]

List After  - [1, 2, 3, 3, 4, 4, 5, 32, 33, 51, 54]

Similar questions