Bubble Sort
Bubble Sort, also known as Exchange Sort, is a simple
sorting algorithm. It works by repeatedly stepping throughout the list to be
sorted, comparing two items at a time and swapping them if they are in the
wrong order. The pass through the list is duplicated until no swaps are desired,
which means the list is sorted.
This is the easiest method among all sorting
algorithms.
Algorithm
Step 1 ➤ Initialization
1.
set 1 ← n, p ← 1
Step 2 ➤ loop,
1.
Repeat through step 4 while (p ≤ n-1)
2.
set E ← 0 ➤ Initializing exchange variable.
Step 3 ➤ comparison, loop.
1.
Repeat for i ← 1, 1, …... l-1.
2.
if (A [i] > A [i + 1]) then
3.
set A [i] ↔ A [i + 1] ➤ Exchanging values.
4.
Set E ← E + 1
Step 4 ➤ Finish, or reduce the size.
1.
if (E = 0) then
2.
exit
3.
else
4.
set l ← l - 1.
How
Bubble Sort Works
1.
The
bubble sort starts with the very first index and makes it a bubble element.
Then it compares the bubble element, which is currently our first index
element, with the next element. If the bubble element is greater and the second
element is smaller, then both of them will swap.
After swapping, the second element will become the bubble element. Now we will
compare the second element with the third as we did in the earlier step and
swap them if required. The same process is followed until the last element.
2.
We
will follow the same process for the rest of the iterations. After each of the
iteration, we will notice that the largest element present in the unsorted
array has reached the last index.
For each iteration, the bubble sort will compare up to
the last unsorted element.
Once all the elements get sorted in the ascending
order, the algorithm will get terminated.
Consider the following example of an unsorted array
that we will sort with the help of the Bubble Sort algorithm.
Initially,

Pass 1:
- Compare
a0 and a1

As a0 < a1 so the array will remain as it is.
- Compare
a1 and a2

Now a1 > a2, so we will swap both of them.

- Compare
a2 and a3

As a2 < a3 so the array will remain as it is.
- Compare
a3 and a4

Here a3 > a4, so we will again swap both of them.

Pass 2:
- Compare
a0 and a1

As a0 < a1 so the array will remain as it is.
- Compare
a1 and a2

Here a1 < a2, so the array will remain as it is.
- Compare
a2 and a3

In this case, a2 > a3, so both of them will get swapped.

Pass 3:
- Compare
a0 and a1

As a0 < a1 so the array will remain as it is.
- Compare
a1 and a2

Now a1 > a2, so both of them will get swapped.

Pass 4:
- Compare
a0 and a1

Here a0 > a1, so we will swap both of them.

Hence the array is sorted as no more swapping is
required.
Complexity
Analysis of Bubble Sort
Input: Given
n input elements.
Output: Number
of steps incurred to sort a list.
Logic: If
we are given n elements, then in the first pass, it will do n-1 comparisons; in
the second pass, it will do n-2; in the third pass, it will do n-3 and so on.
Thus, the total number of comparisons can be found by;

Therefore, the bubble sort algorithm encompasses a time
complexity of O(n2) and a space
complexity of O(1) because it necessitates some
extra memory space for temp variable for swapping.
Time
Complexities:
- Best
Case Complexity: The bubble sort algorithm has a best-case
time complexity of O(n) for the already sorted
array.
- Average
Case Complexity: The average-case time complexity for the
bubble sort algorithm is O(n2), which happens when 2 or more
elements are in jumbled, i.e., neither in the ascending order nor in the
descending order.
- Worst
Case Complexity: The worst-case time complexity is also O(n2), which occurs when we sort the
descending order of an array into the ascending order.
Advantages
of Bubble Sort
1.
Easily
understandable.
2.
Does
not necessitates any extra memory.
3.
The
code can be written easily for this algorithm.
4.
Minimal
space requirement than that of other sorting algorithms.
Disadvantages
of Bubble Sort
1.
It
does not work well when we have large unsorted lists, and it necessitates more
resources that end up taking so much of time.
2.
It
is only meant for academic purposes, not for practical implementations.
3. It involves the n2 order of steps to sort an algorithm.
Comments
Post a Comment