We gonna talk this time about Bubble Sort. Loops are very famous with sorting algorithms. Finding complexities, run time is another thing that we are supposed to know for these sorting algorithms.
For bubble sort, it goes like we scan a list of say numbers or any items, compare the two adjacent elements and swap the two if they are not in order. We might wish the list to be ascending or descending because.
Assuming ascending order, the first pass through the array will place the largest element at the end of the array. And the array under analysis now will be one element short and the process of placing the largest element at the end of the list under scan goes on till we are left with two element array where no comparison is to be made and the largest element is the only element under analysis.
Lets take an example
5 3 1 7 2
First pass : 3 5 1 7 2 = = = = 3 1 5 7 2 = = = = 3 1 5 7 2 = = = = 3 15 2 7
Second Pass , 4 elements under scan since 7 is the largest element and has reached the last position
1 3 5 2 7 = = = = 1 3 5 2 7 = = = = 1 3 2 5 7
Third Pass : 1 3 2 5 7 = = = = 1 2 3 5 7 ( 3 elements were under scan since 5 has reached its position in second pass)
Fourth pass : 1 2 3 5 7
I have tried it in a C program as
I have used Visual C++ version 5.0 for compilation . You can try with any that you are comfortable with.
For this code, you might expect an output like following:
The complexity for best or the worst or an average case even will be of order of O(n2) only for bubble sort.
No of comparisons in last pass would be 1 and in the first pass as n-1 , thus summing up the number of comparisons as :
Therefore, bubble sort is not a practical sorting algorithm when n is large.
Try this at your end to make it more clear.
#include<stdio.h>
#define MAX_SIZE 10
void main()
{
int arr[MAX_SIZE];
int i,j,k;
int temp;
printf("\n enter the array elements");
for(i=0;i<MAX_SIZE;i++)
{
printf("\n enter the %d element :: ",i+1);
scanf("%d",&arr[i]);
}
for(j=0;j<MAX_SIZE-1;j++)
{
for(k=0;k<MAX_SIZE-1-j;k++)
{
// printf("\n nidhi k k+1 %d %d arr[k] %d arr[k+1] %d",k,k+1,arr[k],arr[k+1]);
if(arr[k]>arr[k+1])
{
temp=arr[k];
arr[k]=arr[k+1];
arr[k+1]=temp;
}
}
}
for(i=0;i<MAX_SIZE;i++)
{
printf("\n nidz element[%d ] is : %d ",i+1,arr[i]);
}
getchar();
getchar();
}
You can ask me more at nidhibatra11@gmail.com also.
For bubble sort, it goes like we scan a list of say numbers or any items, compare the two adjacent elements and swap the two if they are not in order. We might wish the list to be ascending or descending because.
Assuming ascending order, the first pass through the array will place the largest element at the end of the array. And the array under analysis now will be one element short and the process of placing the largest element at the end of the list under scan goes on till we are left with two element array where no comparison is to be made and the largest element is the only element under analysis.
Lets take an example
5 3 1 7 2
First pass : 3 5 1 7 2 = = = = 3 1 5 7 2 = = = = 3 1 5 7 2 = = = = 3 15 2 7
Second Pass , 4 elements under scan since 7 is the largest element and has reached the last position
1 3 5 2 7 = = = = 1 3 5 2 7 = = = = 1 3 2 5 7
Third Pass : 1 3 2 5 7 = = = = 1 2 3 5 7 ( 3 elements were under scan since 5 has reached its position in second pass)
Fourth pass : 1 2 3 5 7
I have tried it in a C program as
Bubble sort as it appeared on my editor |
I have used Visual C++ version 5.0 for compilation . You can try with any that you are comfortable with.
For this code, you might expect an output like following:
Output on doing F5. Press any character after seeing this screen. |
The complexity for best or the worst or an average case even will be of order of O(n2) only for bubble sort.
No of comparisons in last pass would be 1 and in the first pass as n-1 , thus summing up the number of comparisons as :

Therefore, bubble sort is not a practical sorting algorithm when n is large.
Try this at your end to make it more clear.
#include<stdio.h>
#define MAX_SIZE 10
void main()
{
int arr[MAX_SIZE];
int i,j,k;
int temp;
printf("\n enter the array elements");
for(i=0;i<MAX_SIZE;i++)
{
printf("\n enter the %d element :: ",i+1);
scanf("%d",&arr[i]);
}
for(j=0;j<MAX_SIZE-1;j++)
{
for(k=0;k<MAX_SIZE-1-j;k++)
{
// printf("\n nidhi k k+1 %d %d arr[k] %d arr[k+1] %d",k,k+1,arr[k],arr[k+1]);
if(arr[k]>arr[k+1])
{
temp=arr[k];
arr[k]=arr[k+1];
arr[k+1]=temp;
}
}
}
for(i=0;i<MAX_SIZE;i++)
{
printf("\n nidz element[%d ] is : %d ",i+1,arr[i]);
}
getchar();
getchar();
}
You can ask me more at nidhibatra11@gmail.com also.
No comments:
Post a Comment