Monday, March 12, 2012

Bubble Sort example program in C

 /*  
 This is a basic program of Bubble sort.  
 Author: Afiz  
 Date:  
 */  
 #include<stdio.h>  
 main()  
 {  
 int a[5]={5,5,1,7,-1}; // array with 5 elements.  
 int i,j; // local variable declarations   
 for(i=0;i<5;i++)// outer loop   
 {  
      for(j=1;j<5-i;j++) // outter loop   
           {  
                if(a[j-1]>=a[j])  
                {  
                     int tmp = a[j-1]; // swap   
                     a[j-1]=a[j];  
                     a[j]=tmp;       
                }  
           }  
 }  
 //printing array   
 for(i=0;i<5;i++)  
 {  
      printf("%d\n",a[i]);   
 }  
 }  

No comments:

Post a Comment