0% found this document useful (0 votes)
4 views9 pages

Understanding Bubble Sort Algorithm

Bubble sort is a sorting algorithm that repeatedly compares and swaps adjacent elements until they are in the correct order, resembling air bubbles rising in water. The process involves multiple iterations, where in each iteration the largest unsorted element is moved to its correct position. The algorithm continues until all elements are sorted in ascending order.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Understanding Bubble Sort Algorithm

Bubble sort is a sorting algorithm that repeatedly compares and swaps adjacent elements until they are in the correct order, resembling air bubbles rising in water. The process involves multiple iterations, where in each iteration the largest unsorted element is moved to its correct position. The algorithm continues until all elements are sorted in ascending order.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Bubble sorting

Computer Programming
ECO 123

12/17/2021 1
Bubble sort is a sorting algorithm that compares two adjacent elements
and swaps them until they are not in the intended order.

Just like the movement of air bubbles in the water that rise up to the
surface, each element of the array move to the end in each iteration.
Therefore, it is called a bubble sort.

12/17/2021 2
Working of Bubble Sort
Suppose we are trying to sort the elements in ascending order.

1. First Iteration (Compare and Swap)


Starting from the first index, compare the first and the second elements.
If the first element is greater than the second element, they are
swapped.
Now, compare the second and the third elements. Swap them if they are
not in order.
The above process goes on until the last element.

12/17/2021 3
12/17/2021 4
2. Remaining Iteration
The same process goes on for the remaining iterations.
After each iteration, the largest element among the unsorted
elements is placed at the end.

12/17/2021 5
In each iteration, the comparison takes place up to the last
unsorted element.

The array is sorted when all the unsorted elements are placed at
their correct positions.

12/17/2021 6
Ex: C Program to arrange an array {-2, 45, 0,
11, -9} in an ascending order using bubble
sorting.

12/17/2021 7
12/17/2021 8
12/17/2021 9

You might also like