Bubble Sort Algorithm
Bubble Sort Algorithm
Step 1 − Check if the first element in the input array is greater than the next element
in the array.
Step 2 − If it is greater, swap the two elements; otherwise move the pointer forward in
the array.
Step 4 − Check if the elements are sorted; if not, repeat the same process (Step 1 to
Step 3) from the last element of the array to the first.
Pseudocode
[Link] 1/6
3/27/26, 7:36 PM Bubble Sort Algorithm
We observe in algorithm that Bubble Sort compares each pair of array element unless
the whole array is completely sorted in an ascending order. This may cause a few
complexity issues like what if the array needs no more swapping as all the elements are
already ascending.
To ease-out the issue, we use one flag variable swapped which will help us see if any
swap has happened or not. If no swap has occurred, i.e. the array requires no more
processing to be sorted, it will come out of the loop.
Analysis
In this algorithm, the number of comparison is irrespective of the data set, i.e. whether
the provided input elements are in sorted order or in reverse order or at random.
Memory Requirement
From the algorithm stated above, it is clear that bubble sort does not require extra
memory.
Example
[Link] 2/6
3/27/26, 7:36 PM Bubble Sort Algorithm
We take an unsorted array for our example. Bubble sort takes (n2) time so we're
keeping it short and precise.
Bubble sort starts with very first two elements, comparing them to check which one is
greater.
In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we
compare 33 with 27.
We find that 27 is smaller than 33 and these two values must be swapped.
Next we compare 33 and 35. We find that both are in already sorted positions.
[Link] 3/6
3/27/26, 7:36 PM Bubble Sort Algorithm
We know then that 10 is smaller 35. Hence they are not sorted. We swap these values.
We find that we have reached the end of the array. After one iteration, the array should
look like this −
To be precise, we are now showing how an array should look like after each iteration.
After the second iteration, it should look like this −
Notice that after each iteration, at least one value moves at the end.
[Link] 4/6
3/27/26, 7:36 PM Bubble Sort Algorithm
And when there's no swap required, bubble sort learns that an array is completely
sorted.
Implementation
One more issue we did not address in our original algorithm and its improvised
pseudocode, is that, after every iteration the highest values settles down at the end of
the array. Hence, the next iteration need not include already sorted elements. For this
purpose, in our implementation, we restrict the inner loop to avoid already sorted
values.
#include<iostream>
using namespace std;
void bubbleSort(int *array, int size){
for(int i = 0; i<size; i++) {
int swaps = 0; //flag to detect any swap is there or not
for(int j = 0; j<size-i-1; j++) {
if(array[j] > array[j+1]) { //when the current item is bigger
than next
int temp;
temp = array[j];
[Link] 5/6
3/27/26, 7:36 PM Bubble Sort Algorithm
array[j] = array[j+1];
array[j+1] = temp;
swaps = 1; //set swap flag
}
}
if(!swaps)
break; // No swap in this pass, so array is sorted
}
}
int main(){
int n;
n = 5;
int arr[5] = {67, 44, 82, 17, 20}; //initialize an array
cout << "Array before Sorting: ";
for(int i = 0; i<n; i++)
cout << arr[i] << " ";
cout << endl;
bubbleSort(arr, n);
cout << "Array after Sorting: ";
for(int i = 0; i<n; i++)
cout << arr[i] << " ";
cout << endl;
}
Output
[Link] 6/6