Programming with Passion

Make the best out of everything.

Thursday 17 March 2016

Bubble Sort

Bubble Sort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int hold;
int array[5];
cout<<"Enter 5 numbers: "<<endl;
for(int i=0; i<5; i++) { cin>>array[i];
}
cout<<endl;
cout<<"Orignally entered array by the user is: "<<endl;
for(int j=0; j<5; j++)
{
cout<<array[j];
cout<<endl;
}
cout<<endl;
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(array[j]>array[j+1])
{
hold=array[j];
array[j]=array[j+1];
array[j+1]=hold;
}
}
}
cout<<"Sorted Array is: "<<endl;
for(int i=0; i<5; i++)
{
cout<<array[i]<<endl;
}
getch();
}
Explanation:-
Now this program will be explained with proper example.
Just consider the array entered by the user is:
Bubble Sort C++
Now the program will use a nested loop to perform sorting. The iterations of outer loop will specified the number of passes and the inner loop will specify the number of iterations.
At the beginning when the loop begins, the value of i=0, Therefore first pass is started.

First Pass:

Bubble Sort C++
In first pass the value of i=0 and the inner loop came into action, it will perform 4 iterations and check the condition of the following block of statements.
1
2
3
4
5
6
if(array[j]>array[j+1])
{
hold=array[j];
array[j]=array[j+1];
array[j+1]=hold;
}
Iteration no. 1:
Bubble Sort C++
At first iteration the value of j=0 and the value of j+1=1, Therefore it will compare the values of zero index and the first index of an array. As we can see that the value at Zero index is smaller than first index, therefore the array remains the same.
Iteration no.2:
Bubble Sort C++
At second iteration the value of j=1 and the value of j+1=2, Therefore it will compare the values of first index and the second index of an array. As we can see that the value at second index is smaller than first index, therefore the elements of first and second index will be swap, the new array looks like.
Bubble Sort C++
Iteration No.3:
At Third iteration the value of j=2 and the value of j+1=3, Therefore it will compare the values of second index and the Third index of an array. As we can see that the value at third index is smaller than second index, therefore the elements of third and second index will be swap, the new array looks like.
Bubble Sort in C++

Iteration no.4:
At fourth iteration the value of j=3 and the value of j+1=4, Therefore it will compare the values of third index and the fourth index of an array. As we can see that the value at fourth index is smaller than third index, therefore the elements of fourth and third index will be swap, the new array looks like.
Bubble Sort in C++
As you can see that at the end of the first pass the largest value is placed at the last index.

Second Pass:


Iteration no. 1:

Bubble Sort in C++
At first iteration the value of j=0 and the value of j+1=1, Therefore it will compare the values of zero index and the first index of an array. As we can see that the value at Zero index is smaller than first index, therefore the array remains the same.

Iteration no.2:

Bubble Sort in C++
At second iteration the value of j=1 and the value of j+1=2, Therefore it will compare the values of first index and the second index of an array. As we can see that the value at second index is greater than first index, therefore the array will be same.

Iteration No.3:

C++ Bubble Sort
At Third iteration the value of j=2 and the value of j+1=3, Therefore it will compare the values of second index and the Third index of an array. As we can see that the value at third index is smaller than second index, therefore the elements of third and second index will be swap, the new array looks like.
C++ Bubble Sort

Iteration no.4:

C++ Bubble Sort
At fourth iteration the value of j=3 and the value of j+1=4, Therefore it will compare the values of third index and the fourth index of an array. As we can see that the value at fourth index is greater than third index, therefore the array will remains the same.
C++ Bubble Sort
As you can see that at the end of the second pass the second largest element is places at the second last index of an array.

Third Pass:


Iteration no. 1:

C++ Bubble Sort
At first iteration the value of j=0 and the value of j+1=1, Therefore it will compare the values of zero index and the first index of an array. As we can see that the value at Zero index is smaller than first index, therefore the array remains the same.

Iteration no.2:

Bubble sorting C++
At second iteration the value of j=1 and the value of j+1=2, Therefore it will compare the values of first index and the second index of an array. As we can see that the value at second index is smaller than first index, therefore the elements will swap and new array will be look like.
Bubble sorting C++

Iteration No.3:

Bubble sorting C++
At Third iteration the value of j=2 and the value of j+1=3, Therefore it will compare the values of second index and the Third index of an array. As we can see that the value at third index is greater than second index, therefore the array remains the same.
Bubble sorting in C++

Iteration no.4:

Bubble sorting in C++
At fourth iteration the value of j=3 and the value of j+1=4, Therefore it will compare the values of third index and the fourth index of an array. As we can see that the value at fourth index is greater than third index, therefore the array will remains the same.
C++ Bubble Sorting
As you can see that at the end of the third pass the second largest element is places at the third last index of an array.

Fourth Pass:

Iteration no. 1:

C++ Bubble Sorting
At first iteration the value of j=0 and the value of j+1=1, Therefore it will compare the values of zero index and the first index of an array. As we can see that the value at Zero index is greater than first index, therefore the elements will swap and new array will be look like.
C++ Bubble Sorting
At this point the loop is completed and the fourth last value is placed at the fourth last index of an array. Which means the position of fourth value is final, the position of fifth value is automatically finalized because it automatically placed at index one. Now at the end outer loop will also terminate and we will get the perfectly sorted array in ascending order with the help of Bubble sort technique.

No comments:

Post a Comment