Coders Packet

Bubble Sort In Python

By SRIJAN MAJUMDAR

The project implements the bubble sort algorithm using Python programming language. Bubble sort is used to sort the elements of the array and known as one of the stable sort algorithms.

Bubble sort is one of the most important algorithms to sort the elements of the array in their proper position. In this sorting approach, we use two loops for sorting the array. The first array is for the number of passes throughout the loop and the inner loop is for the comparisons of the elements. So, the worst-case time complexity for implementing the bubble sort is O(N^2) where N is the size of the array or the input size. In the first pass, we get the largest element of the array in its last position, so the first element is bubbled down or bubbled up. Similarly, for the second pass, we get the second-largest element of the array. So, mathematically it becomes n+(n-1)+(n-2)+.....1 = n(n+1)/2 = O(N^2) asymptotically. Below is the bubble sort code.

Bubble_Sort_Code

In the above example: There are 5 numbers of passes something like this: Pass1: -90 -99 -45 35 21 94 82 4500, as -90>-99 swap the array elements becomes -99 -90 -45 35 21 9482 4500 again 35>21 swap -99 -90 21 35 9482 4500 again 9482>4500 swap -99 -90 -45 21 35 4500 9482. After pass 1 the array becomes  -99 -90 -45 21 35 4500 9482 which is sorted. For the remaining passes, the array becomes as, Pass2: -99 -90 -45 21 35 4500 9482 Pass3:-99 -90 -45 21 35 4500 9482, Pass4:-99 -90 -45 21 35 4500 9482, Pass5:-99 -90 -45 21 35 4500 9482. Finally the sorted array of the above unsorted array is:-99 -90 -45 21 35 4500 9482. 

Bubble_Sort_Output

 

 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by SRIJAN MAJUMDAR (CoderSkilsm)

Download packets of source code on Coders Packet