bubble sort in c ++

Bubble sort 



#include<iostream.h>                                                          
void bubblesort(int[], int);                                                  
void main()                                                                   
{                                                                               
     int ar[50], item, n, index;                                                  
     cout<<"\n how many elements do u want to create array      with?...";  
           
     cin>>n;                                                                     
     cout<<"\n enter aray elements..\n";                                          
     for(int i=0; i<n; i++)                                                         
     cin>>ar[i];                                                                
     bubblesort(ar, n);                                                           
    cout<<"\n the sorted array is as shown  below...\n";     
                  
    for(i=0; i<n; i++)                                                              
    cout<<ar[i]<<" ";                                                          
    cout<<endl;                                                                 
}                                                                             
 void bubblesort (int ar[], int size)                                          
 {                                                                              
        int tmp, ctr=0;
        for(int i=0; i<size; i++)                                                    
        {                                                                               
            for(int j=0;j<(size-1);j++)                                                  
            {                                                                              
                 if(ar[j]>ar[j+1])                                                             
                 {                                                                              
                     tmp=ar[j];                                                                   
                     ar[j]=ar[j+1];                                                               
                     ar[j+1]=tmp;                                                               
                 }                                                                          
            }                                                                            
        cout<<" array after iteration -"<<++ctr<<" -is:";                             

             for(int k=0; k<size;k++)                                                       
             cout<<ar[k]<<" ";                                                          
             cout<<endl;                                                                 
        }                                                                          
 }                                                                                                                                                          

Post a Comment

0 Comments