- 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
#include<iostream>
|
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[0]= 5
array[1]= 4
array[2]= 3
array[3]= 2
array[4]=-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= 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.
Hope this will be helpful
Note there are always more than one ways to code a program
do experiment with code and keep learning.
image view of code:
thank you so much...^_^
ReplyDeletecan you help me
Deletecan you help me
DeleteThanks
ReplyDeleteExplanation was too good....
ReplyDeletehow can I display the pass?
ReplyDeletethank u sir
ReplyDeletegood
Deletewhat should i change if i want to output is names??
ReplyDeleteUse char data type
Deletehey guys! i need help..this is my problem...How to program a function using sorting algorithm.i need codes.
ReplyDeleteThank you so so much!
ReplyDeleteinsertion sort ka be program create kar do
ReplyDeletePlease solve question and sent here
ReplyDeleteQ: Find array length = n using bubble sort
please waiting for you reply