V-Lab @ ANDC

Bubble Sorting

Aim

Sorting of an array using Bubble sorting

Theory

Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order. This algorithm is not suitable for large data sets as its average and worst case complexity are of O(n2) where n is the number of items.

Procedure

    Suppose we are trying to sort the elements in ascending order.

  1. First Iteration (Compare and Swap)
    1. Starting from the first index, compare the first and the second elements.
    2. If the first element is greater than the second element, they are swapped.
    3. Now, compare the second and the third elements. Swap them if they are not in order.
    4. The above process goes on until the last element.
  2. Remaining Iteration
  3. The same process goes on for the remaining iterations.
    After each iteration, the largest element among the unsorted elements is placed at the end.

    In each iteration, the comparison takes place up to the last unsorted element.

    The array is sorted when all the unsorted elements are placed at their correct positions.

Practice

Enter an array:
For example if array is [1,2,3,4]
simply enter 1,2,3,4

Here's the result




Python Code

                        
                            array= eval(input("Enter the array for sorting: "))

                            def bSort(array):
                                for i in range(len(array)):
                                    for j in range(len(array)-i-1):
                                        if(array[j]> array[j+1]):
                                            hey= array[j]
                                            array[j]= array[j+1]
                                            array[j+1]= hey
                                            print(array)

                            bSort(array)
                            print(array)

                        
                    

Result

Hence we can perform sorting of arrays using Bubble sorting.

Team

Mr. Nilesh Pandey, B.Sc(H) Computer Science, II year,
Ms. Ananya Shukla, B.Sc(H) Computer Science, II year,
Mr. Amitesh Kumar Singh, B.Sc(H) Computer Science, II year.

Mentors:
Prof. Sharanjit Kaur,
Ms. Nishu Singh