# Bubble Sort in C++

In this tutorial, we will be learning how to implement bubble sort in C++.

‘Sorting’ in programming refers to the proper arrangement of the elements of an array (in ascending or descending order).

Note: ‘array’ is a collection of variables of the same data type which are accessed by a single name.

‘Bubble Sort’ uses the following algorithm to  sort the elements of an array:
let the array be -> {1,5,3,2}

1. compare the first 2 elements and then swap their positions if the element at the 2nd position is smaller (or greater) than the element at the 1st position, to sort in ascending (or descending) order.
1<5, so array -> {1,5,3,2}
2. repeat step 1 for the elements at the 2nd and the 3rd position.
3<5, so array -> {1,3,5,2}
3. repeat step 1 for all the other consecutive-pair elements of the array.
2<5, so array -> {1,3,2,5}
4. reduce the effective length of the array by 1  and go back to step 1 if at least one comparison can still be made.
5 is now ignored as the effective length is reduced by 1
1<3, so array -> {1,3,2,5}
2<3, so array -> {1,2,3,5}
is now ignored as the effective length is reduced by 1 again
1<2, so array -> {1,2,3,5}
is now ignored as the effective length is reduced by 1 again
Since no further comparisons can be made, the sorted array is -> {1,2,3,5}

## C++ code to implement Bubble Sort

```#include <stdio.h>
#define size 4 //pre-defining the array length for the program
void inputArray(int array[],int length)
{
printf("Input %d (integer) elements: ",length);
//note, in the for loop, the intitializing statement is decreasing the value of length by 1 because in arrays, counting starts from 0
for(length--;length>=0;length--){
scanf("%d",&array[length]);
}
}
//logic for bubble sort - comparing two adjacent array elements
void bubbleSort(int array[], int length)
{
for(int i = 0; i < length-1; i++){
//note, before every 'i' iteration of the first for loop, the 'i' number of elements from the end are perfectly sorted, so no need to execute the second for loop for an extra 'i' number of times
for(int j=0;j<length-i-1;j++)
if(array[j]>array[j+1]){//swapping
array[j]+=array[j+1];
array[j+1]=array[j]-array[j+1];
array[j]-=array[j+1];
}
}
}
void printArray(int array[],int length)
{
printf("The elements of the given array are: ");
for(int i=0;i<length;i++)printf("%d ",array[i]);
}
int main()
{
int array[size];//creating an integer array
inputArray(array,size);//taking input in the array
bubbleSort(array,size);//sorting the array with the desired sorting algorithm
printArray(array,size);//printing the array after sorting
return 0;
}```

Output:

```Input 4 (integer) elements: 1 5 3 2
The elements of the given array are: 1 2 3 5```

Summary:

• inputArray(int[],int) is called to take input integer elements from the user and store them in the array
• bubbleSort(int[],int) is called to sort the elements of the array with the algorithm explained above
• printArray(int[],int) is called to print the elements of the array (after sorting)

Note:

• the value ‘4’ after size can be changed to any other value in order to get the desired array-length
• since (in C++) arrays are passed by reference, therefore the changes made in bubbleSort(int[],int) stay permanently
• to sort in descending order, just change the sign from ‘>’ to ‘<‘ in the logical condition inside the 2nd for loop

Also learn: