# Bubble sort in c++ code example

Tutorial contains
• Bubble sort c++ code
• Code dry run with explanation
• Image view of code

Concept used for bubble sort in this examples are:

•     int array
•     nested for loop
•     if statement

C++ code

 `#includeusing namespace std;int main(){     //declaring array      int array;      cout<<"Enter 5 numbers randomly : "<>array[i];              }        cout<array[j+1])       {        temp=array[j];        array[j]=array[j+1];        array[j+1]=temp;               }     }            }    // Displaying Sorted array      cout<<"  Sorted Array is: "<

In order to understand bubble sort for a c++ beginner one must dry run the code
on paper to understand what is actually happening in the code

Dry run of code with example

size of the array is 5 you can change it with your
desired size of array
Input array is

5  4  3  2  -5

so values on indexes of array is
array= 5
array= 4
array= 3
array= 2
array=-5

In nested for loop bubble sort is doing its work

outer loop variable is i2 will run form 0 to 4
inner loop variable is  j will run from 0 to 3

Note for each i2 value inner loop will run from 0 to 3

like when
i2=0  inner loop  0 -> 3
i2=1  inner loop  0 -> 3
i2=2  inner loop  0 -> 3
i2=3  inner loop  0 -> 3
i2=4  inner loop  0 -> 3

input 5  4  3  2  -5
for i2= 0;
j=0
array[j]>array[j+1]
5    >   4        if condition true

here we are swapping 4 and 5
array after 4 5 3 2 -5

j=1
array[j]>array[j+1]
5    >   3        if condition true

here we are swapping 3 and 5
array after 4 3 5 2 -5

j=2
array[j]>array[j+1]
5    >   2        if condition true

here we are swapping 2 and 5
array after 4 3 2 5 -5

j=3
array[j]>array[j+1]
5    >   -5        if condition true

here we are swapping -5 and 5
array after 4 3 2 -5 5

first iteration completed
---------------------------------------------------------------------------------------------------------------------

for i2= 1;
j=0
array[j]>array[j+1]
4    >   3        if condition true

here we are swapping 4 and 3
array after 3 4 2 -5 5

j=1
array[j]>array[j+1]
4    >   2        if condition true

here we are swapping 4 and 2
array after 3 2 4 -5 5

j=2
array[j]>array[j+1]
4    >   -5        if condition true

here we are swapping 4 and -5
array after 3 2 -5 4 5

j=3
array[j]>array[j+1]
4    >   5        if condition FALSE

no swapping
array after 3 2 -5 4 5

--------------------------------------------------------------------------------------------------------------

for i2= 2;
j=0
array[j]>array[j+1]
3    >   2        if condition true

here we are swaping 3 and 2
array after 2 3 -5 4 5

j=1
array[j]>array[j+1]
3    >   -5        if condition true

here we are swaping 3 and -5
array after 2 -5 3 4 5

j=2
array[j]>array[j+1]
3    >   4        if condition FALSE

no swapping
array after 2 -5 3 4 5

j=3
array[j]>array[j+1]
4    >   5        if condition FALSE

no swapping
array after 2 -5 3 4 5 a

--------------------------------------------------------------------------------------------------------------------

for i2= 3;
j=0
array[j]>array[j+1]
2    >   -5      if condition true

here we are swaping 2 and -5
array after -5 2 3 4 5

j=1
array[j]>array[j+1]
2    >   3       if condition FALSE

no swapping
array after -5 2 3 4 5

at this point we can see that our array is sorted

but the code will continue to run for remaining iterations
and every time if condtion will be FALSE because we have got
required ascending sorted array.

Note there are always more than one ways to code a program
do experiment with code and keep learning.

image view of code:

Output:

for more examples:
have a look here      C++ Simple Examples