Bubble sort is the simplest Sorting algorithm available. It works by repeatedly swapping the adjacent or neighbor element if they are in the wrong order.

Working

Actually in the first-pass bubble sort sets the largest element to its position, in the second pass algorithm sets the second largest element this continues till the list or array is not finally sorted.

Here is the example image.

working of bubble sort

To work with the algorithm you first have to understand what is swapping and how it is done.

What is Swapping ??

Swapping is a technique to interchange the content of the two variables, using another variable called a temporary variable.

In python programming language swapping works in two ways, both of them are shown below.

swapping

For the program in Python

arr = [64, 34, 25, 12, 22, 11, 90]  
  
n = len(arr)  
  
for i in range(n):  
  
  # Last i elements are already in place  
  for j in range(0, n-i-1):  
  
    # traverse the array from 0 to n-i-1  
    # Swap if the element found is greater  
    # than the next element  
    if arr[j] > arr[j+1] :  
      arr[j], arr[j+1] = arr[j+1], arr[j]  
  
print ("Sorted array is:")  
for i in range(len(arr)):  
 print ("%d" %arr[i]),   
  

that’s all for this post.
if you have liked this post please subscribe and share the post.
Thanks for being here.