Task :

Python program to for Bubble Sort

Time Complexity of Bubble Sort

Best CaseO(n)
Average CaseO(n2)
Worst CaseO(n2)

Algorithm:

Given a list L of n elements with values or records L0, L1, …, Ln-1, bubble sort is applied to sort the list L.
  1. Compare first two elements L0, L1 in the list.
  2. if L1 < L0, swap those elements and continue with next 2 elements.
  3. Repeat the same step until whole the list is sorted, so no more swaps are possible.
  4. Return the final sorted list.

def bubble_sort(sort_list):

    for j in range(len(sort_list)):

        for k in range(len(sort_list) - 1):

            if sort_list[k] > sort_list[k + 1]:

                sort_list[k], sort_list[k + 1] = sort_list[k + 1], sort_list[k]

    print(sort_list)



lst = []

size = int(input("Enter size of the list: \t"))


for i in range(size):

    elements = int(input("Enter the element: \t"))

    lst.append(elements)


bubble_sort(lst)

No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...