# Python Program: Bubble Sort

Bubble Sort, also known as Sinking Sort, is a sorting agorithm in which numbers are sorted by repeated comparison of a pair of consecutive numbers and swapping them if they are in wrong order.

We consider the below sequence of unsorted numbers:

( 7 3 5 -9 )

Here we will next illustrate the use of Bubble Sort to reorder the above given set of numbers in ascending order, resulting in them being finally sorted as follows.

( -9 3 5 7 )

## First Iteration

We first hook the first number from the left, which is 7. Comparison begins with the next consecutive number 3, and then the next number 5, till the last number -9 is reached, swapping places whenever the number on the right is smaller.

( **7** **3** 5 -9 ) → ( **3** **7** 5 -9 ), compare the first two numbers; makes a swap since 7 > 3

( **3** 7 **5** -9 ) → ( **3** 7 **5** -9 ), makes no swap since 3 < 5

( **3** 7 5 **-9** ) → ( **-9** 7 5 **3** ), makes a swap since 3 > -9.

## Second Iteration

Here, the second number is hooked. Comparison starts with the next consecutive number till the last, swapping places whenever the number on the right is smaller.

( -9 **7** **5** 3 ) → ( -9 **5** **7** 3 ), makes a swap since 7 > 5

( -9 **5** 7 **3** ) → ( -9 **3** 7 **5** ), makes no swap since 5 > 3

## Third Iteration

The third number is hooked, which is the second number to last. Comparison starts with the next consecutive number, which is 5 and the last. Since the number on the right is smaller, swapping takes place. This is the last iteration of the Bubble Sort algorithm.

( -9 3 **7** **5** ) → ( -9 3 **5** **7** ), makes a swap since 7 > 5

After this last iteration, we get our sorted numbers as:

( -9 3 5 7 )

We write a Python Program below based on the above sorting algorithm.

The numbers ( -9 3 5 7 ) are assigned to a pre-defined list called `numbers`

as elements. Also, there's a third variable called `temp`

used inside the innermost `for`

loop to temporarily store assigned numbers while swapping.

` ````
numbers = [7,3,5,-9]
# bubble sort
for i in range(0,len(numbers)-1):
for j in range(i+1,len(numbers)):
if(numbers[i] > numbers[j]):
temp = numbers[j]
numbers[j] = numbers[i]
numbers[i] = temp
# printing the sorted array
for i in range(0,len(numbers)):
print(numbers[i]," ", end='')
```

# Read Numbers Dynamically

Instead of feeding hard-coded sequence of numbers to the list, we can enhance the above Python program to take numbers dynamically.

` ````
numbers = []
n = int(input("How many numbers to sort? "))
for i in range(0,n):
j = int(input('numbers[' + str(i) + ']= '))
numbers.append(j)
# bubble sort
for i in range(0,len(numbers)-1):
for j in range(i+1,len(numbers)):
if(numbers[i] > numbers[j]):
temp = numbers[j]
numbers[j] = numbers[i]
numbers[i] = temp
# printing the sorted array
for i in range(0,len(numbers)):
print(numbers[i]," ", end='')
print()
```

We run the above program, and the prompt to enter the total number of numbers to sort appears first. We then enter the numbers individually.

Practically, Insertion Sort is much efficient compared to Bubble Sort, despite both having the same complexity order of $\text{O}(n^{2})$.