Today, we will discuss Bubble Sort Algorithm and Program Logic in CPP ( C++ ) programming language. We will study:
- Definition of Bubble Sort Algorithm
- Trace steps for sample data or working of Bubble sorting algorithm
- Algorithm of Bubble Sorting Method
- Actual code of Bubble sort program in C++ programming language
What is Bubble Sort?
Bubble sort is a sorting algorithm, in which we repeatedly pass through the whole array and swap the adjacent elements if they are not sorted.Note that: In Bubble sort there will be maximum (n-1) passes to sort the given array. For example, if array size n = 5 then there will be (n-1) that is 4 passes.
Note that: In first pass there will be 4 comparisons like 5>4. In second pass there will be 3 comparisons. In third pass there will be two comparisons. And in last that is 4th pass there will be only one comparison.
Method of Bubble Sorting
- 1. Compare first two adjacent elements, if first is greater than second then swap them( if sorting on ascending order)
- 2. Repeat this comparison for second and third adjacent elements, then third and fourth elements and so on to second last and last elements. And swap the values where needed. This makes ONE PASS of Bubble Sort Algorithm.
- 3. Now again start with first and second element to compare and swap if first is greater than other. This time ignore last element. So you will compare adjacent elements from number 1 to second last element.
- 4. Similarly in third pass, you will reduce another comparison from end, and so on.
- 5. Keep repeating for one fewer element each time until there will be no pairs to compare. In Last Pass, there will be only one comparison of first and second element.
Bubble Sort Algorithm and Program Logic in CPP - on Sample Data
For example:
Let given un sorted array is:
5, 4, 3, 2, 1
In first pass:
5, 4, 3, 2, 1
Pass 1:
We will check if 5>4 then swap(5,4) so that array will become as follows
4, 5, 3, 2, 1
Next we will check if 5>3 then swap(5,3) so that array will become:
4, 3, 5, 2, 1
Next we will check if 5>2 then swap(5,2) so that array will become:
4, 3, 2, 5, 1
Next we will check that if 5>1 then swap(5,1) so that array will become:
4, 3, 2, 1, 5
Note: You should note that in pass 1, the last element (5) is bubbled and placed to its correct location(sorted) in the given array.
Pass 2:
4, 3, 2, 1, 5
We will check if 4>3 then swap(4,3) which is true , so that array will become as follows:
3, 4, 2, 1, 5
Next we will check if 4>2 then swap(4,2) so that array will become:
3, 2, 4, 1, 5
Next we will check if 4>1 then swap(4,1) so that array will become:
3, 2, 1, 4, 5
Note: You should note that in pass 2, the second last element (4) is bubbled and placed to its correct location(sorted) in the given array.
Pass 3:
3, 2, 1, 4, 5
We will check if 3>2 then swap(3,2) which is true , so that array will become as follows:
2, 3, 1, 4, 5
Next we will check if 3>1 then swap(3,1) so that array will become:
2, 1, 3, 4, 5
Note: You should note that in pass 3, the third last element (3) is bubbled and placed to its correct location(sorted) in the given array.
Pass 4:
2, 1, 3, 4, 5
We will check if 2>1 then swap(2,1) which is true so the array becomes:
1, 2, 3, 4, 5
Which is the required sorted array.
Bubble Sort Algorithm and Program Logic in CPP
Bubble Sort Algorithm
This algorithm will input values in array A of n size. Then sort the array by Bubble sort method and display the display the array. Suppose A be a linear array of n numbers. Temp is a temporary variable for swapping (interchanging) the position of the numbers in array as required according to the sorting order.
1. Input n numbers of an array A
Initialise i=0 and repeat through 1b if (i<n)
1a) Display Message " Enter a number="
1b) Input A[i]
2. Initialise i= 0 and repeat through step 4 if (i< n)
3. Initialize j= 0 and repeat through step 4 if (j< n – i– 1)
4. If (A[j] > A[j + 1])
(a) Temp = A[j]
(b) A[j] = A[j + 1]
(c) A[j+ 1] = Temp
5. Display the sorted numbers of array A
Initialise i=0 and repeat through 5a if (i<n)
5a) Display A[i]
6. Exit.
Picture: Bubble Sort Algorithm and Program Logic in CPP
Bubble Sort Algorithm, Trace Steps on Sample data with C++ Program |
Bubble Sort Program Code for Array of size s = 5 integer elements
Sample Run in Turbo C++ 3.0 IDE Bubble Sort Algorithm and Program Logic in CPP
Algorithm of Bubble Sort, Tracing Steps on Sample data with C++ Program |
(c)EasyCppProgramming.Blogspot.Com
C++ is Simplified Here!
Target 1001 C++ Example Programs!
EasyCppProgramming.Blogspot.Com
*/
// Program To Input an array and sort
// the array by Bubble Sort Algorithm
#include<iostream>
#include<conio.h>
int main()
{
int a[5], s=5, i, j, k, temp;
clrscr();
// input array
cout<<"Enter 5 array elements to sort:\n";
for(i=0;i<s;i++)
cin>>a[i];
// show un-sorted array
cout<<"\n Un-Sorted array is =";
for(i=0;i<s;i++)
cout<<a[i]<<" ";
cout<<"\n------------------------------------";
// sort array by bubble sort
for(i=0;i<s-1;i++)
{
for(j=0;j<(s-1-i);j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
cout<<"\nAfter Pass "<<(i+1)<<" elements are : ";
for (k = 0; k < s; k++)
cout<<a[k]<<" ";
cout<<endl;
}/*End of outer for loop*/
// show sorted array
cout<<"\n ----------------------------------";
cout<<"\nSorted Array is = ";
for(i=0;i<s;i++)
cout<<a[i]<<" ";
// wait for user to press any key
getch();
// end
return 0;
}
Now after studying Bubble Sort Algorithm and Program Logic in CPP, Read more on
0 comments:
Post a Comment
We Love To Hear From You!