Bubble Sort

This is a C program to demonstrates a type of sorting of an array known as BUBBLE SORT.

You can compile this program on bOtskOOl Free Online C/C++ Compiler

 

 

SOURCE CODE

 

//A C example for BUBBLE SORT
 
/*
**********************
www.botskool.com
**********************
*/
 
#include<stdio.h>
//#include<conio.h>
void main()
{
int i,j,n,*a,temp;
clrscr();
printf("How many Elements do you wish to add in array? :");
scanf("%d",&n);
 
printf("\nEnter the elements:\n");
for(i=0;i<n;i++)
{
                printf("Enter Element no %d : ",1+i);
                scanf("%d",&a[i]);
}
printf("\n Original ARRAY: \n");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
 
for(i=1;i<=n-1;i++)
{
                for(j=0;j<n-i;j++)
                if(a[j]>a[j+1])
                {
                                temp=a[j];
                                a[j]=a[j+1];
                                a[j+1]=temp;
                }
}
 
 
 
printf("\n\n SORTED ARRAY: \n");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
getch();
}
 
/*
**********************
www.botskool.com
**********************
*/

 

Output of the above program is shown below:

Output - Bubble Sort

 

Download source code.

 

 

Terms of Agreement:  
By using this code, you agree to the following terms-  
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.  
2) You MAY NOT redistribute this code (for example to a web site) without written permission from us. Failure to do so is a violation of copyright laws.  
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which may have placed in the code or code's description.

 >>Kindly post your doubts and suggestions on our discussion forum.

Tags: