Here’s a step-by-step explanation of the Bubble Sort algorithm with an example:

**1.**Start at the beginning of the list.**2.**Compare the first two elements. If the first element is greater than the second element, swap them.**3.**Move to the next pair of elements and repeat step 2.**4.**Continue this process until you reach the end of the list. After the first pass, the largest element will have “bubbled” to the end of the list.**5.**Repeat the above steps for the remaining elements, excluding the last element (which is already in its correct position).**6.**Continue these passes until no more swaps are needed. This indicates that the list is sorted.` ````
```begin BubbleSort(arr)
for all array elements
if arr[i] > arr[i+1]
swap(arr[i], arr[i+1])
end if
end for
return arr
end BubbleSort

**Example 1:**

Let’s say we have an unsorted list of numbers:

[5, 3, 1, 4, 2]

**First Pass:**

The list becomes [3, 5, 1, 4, 2]

The list becomes [3, 1, 5, 4, 2]

The largest element (5) is now at the end of the list.

**Second Pass:**

The second largest element (4) is now in its correct position.

**Third Pass:**

The third largest element (3) is now in its correct position.

**Fourth Pass:**

The list becomes [1, 2, 3, 4, 5].

The fourth largest element (2) is now in its correct position.

**Fifth Pass:**

No swaps are needed in this pass. The list is already sorted.

## Various Sorting Algorithm

- Bubble sort
- Modified Bubble Sort
- Selection sort
- Insertion sort
- Quick sort
- Merge sort
- Heap sort

## Bubble Sort

n=7

0 1 2 3 4 5 6

25 37 81 19 44 50 62

25 37 19 44 50 62 81

25 19 37 44 50 62

19 25 37 44 50

19 25 37 44

19 25 37 44 50 62 81(0,1)(1,2)(4,3)(3,4)(4,5)(5,6)

(0,1)(1,2)(4,3)(3,4)(4,5)

(0,1)(1,2)(2,3)(4,5)

(0,1)(1,2)(2,3)

(0,1)(1,2)

(0,1)

## Modified Bubble Sort

flag=0

if (a[i]>a[i+1])

{

//swap

flag=1;

}