Bubble Sort Algorithm
Bubble Sort Algorithm
Bubble Sort Algorithm
We assume list is an array of n elements. We further assume that swap function swaps
the values of the given array elements.
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
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.
https://www.tutorialspoint.com/data_structures_algorithms/bubble_sort_algorithm.htm 1/6
Page 2 of 6
Analysis
Here, the number of comparisons are
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
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.
https://www.tutorialspoint.com/data_structures_algorithms/bubble_sort_algorithm.htm 2/6
Page 3 of 6
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.
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 −
https://www.tutorialspoint.com/data_structures_algorithms/bubble_sort_algorithm.htm 3/6
Page 4 of 6
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.
And when there's no swap required, bubble sort learns that an array is completely sorted.
https://www.tutorialspoint.com/data_structures_algorithms/bubble_sort_algorithm.htm 4/6
Page 5 of 6
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.
Open Compiler
#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];
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++)
https://www.tutorialspoint.com/data_structures_algorithms/bubble_sort_algorithm.htm 5/6
Page 6 of 6
Output
Array before Sorting: 67 44 82 17 20
Array after Sorting: 17 20 44 67 82
https://www.tutorialspoint.com/data_structures_algorithms/bubble_sort_algorithm.htm 6/6