Bubble Sort
Bubble Sort
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.
1. Starting from the first index, compare the first and the second
elements.
2. If the first element is greater than the second element, they are
swapped.
3. Now, compare the second and the third elements. Swap them if they
are not in order.
4. The above process goes on until the last element.
Compare the
Adjacent Elements
2. Remaining Iteration
After each iteration, the largest element among the unsorted elements is
placed at the end.
Put the largest element at
the end
The array is sorted when all the unsorted elements are placed at their
correct positions.
The array is sorted if all elements are kept in the right order
// Bubble sort in C
#include <stdio.h>
array[i + 1] = temp;
// print array
printf("\n");
int main() {
bubbleSort(data, size);
printArray(data, size);