# C++ Program to implement bubble sort in C++

Here we are going to implement the bubble sorting technique in c++. This program is to sort an array in ascending order using the bubble sort technique.

## Bubble sort in C++

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity is quite high.

## How Bubble Sort Works?

Consider an array arr[] = {5, 1, 4, 2, 8}
First Pass:
•    Bubble sort starts with the very first two elements, comparing them to check which one is greater.
•    ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, the algorithm compares the first two elements and swaps since 5 > 1.
•    ( 1 5 4 2 8 ) –>  ( 1 4 5 2 8 ), Swap since 5 > 4
•    ( 1 4 5 2 8 ) –>  ( 1 4 2 5 8 ), Swap since 5 > 2
•    ( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), the algorithm does not swap them.
Second Pass:
•    Now, during the second iteration it should look like this:
•    ( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
•    ( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
•    ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
•    ( 1 2 4 5 8 ) –>  ( 1 2 4 5 8 )
Third Pass:
•    Now, the array is already sorted, but our algorithm does not know if it is completed.
•    The algorithm needs one whole pass without any swap to know it is sorted.
•    ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
•    ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
•    ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
•    ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

## C++ program for implementing bubble sort

The user is asked to enter the size of the array and its elements. The size entered by the user is read into the variable n. The elements to sort are stored in an array called arr[50].To get the elements into the array a` for loop` can be used.

``````
for(i=0; i>arr[i];
```
```

initially, the value of i is set to 0 and checked for the condition i if true then get the first element into arr[i] and increment the value of i.
now all the n elements are get stored in the arr[]. for sorting using `for loop`,

```
```
for(i=0; i<(n-1); i++)

{

for(j=0; j<(n-i-1); j++)

{

if(arr[j]>arr[j+1])

{

temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

}
```
```

initially i=0 and checks the condition, whether it is less than n-1 or not. If the condition evaluates to be true, program flows go inside the loop.   j=0 (initially) and the condition j<(n-i-1) evaluates to be true, the program flow goes inside the loop and checks the condition of if block, that is arr[j]>arr[j+1] evaluates to be true, the program flow goes inside the if block.  the element at index number j gets swapped with the element at index number j+1. Now program flow goes to the updation part of the inner` for loop` and increments the value of j. Again checks the condition, that is j<(n-i-1) evaluates to be true, therefore program flow again goes inside the loop and checks the condition of the if block. If the condition evaluates to be true, then process the three statements, otherwise update the value of j, and check the condition Continue the process similar with the updated value, until the condition of the outer` for loop `evaluates to be false. If the condition of the outer for loop evaluates to be false, then program flow exits the loop Now the array arr[] gets sorted, just print it.

### Algorithm

Step 1: Call the header file` iostream.`

Step 2: Use the `namespace std`.

Step 3: Open the integer type main function; int main().

Step 4: Declare integer type variables n, i, j. arr[50], temp;

Step 5: Ask the user to enter the size of the array;

Step 6: Get the number into the variable n;

Step 7: Ask the user to enter the numbers;

Step 8: Get the numbers into the array arr[50];

Step 9: Get the first two elements of the array and check the condition arr[0] > arr[1];

Step 10: swap the elements.

Step 11: Get the elements arr[1] and arr[2]; check the condition arr[1]>arr[2];

Step 12: Repeat step 10;

Step 13: Likewise get each element and compare;

Step 14: print the sorted array;

Step 15: Exit;

## C++ Source Code

```                                          ```#include<iostream>
using namespace std;
int main()
{
int n, i, arr[50], j, temp;
cout<<"Enter the Size (max. 50): ";
cin>>n;
cout<<"Enter "<<n<<" Numbers: ";
for(i=0; i<n; i++)
cin>>arr[i];
cout<<"\nSorting the Array using Bubble Sort Technique..\n";
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
cout<<"\nArray Sorted Successfully!\n";
cout<<"\nThe New Array is: \n";
for(i=0; i<n; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}```
```

## OUTPUT

```Enter the Size (max. 50): 6
Enter 6 Numbers: 6
2
8
3
9
1
Sorting the Array using Bubble Sort Technique..

Array Sorted Successfully!

The New Array is:
1 2 3 6 8 9
```
VIEW ALL
VIEW ALL
VIEW ALL