Tuesday 9 June 2015

Bubble sort in c++

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
 int bubble[10];
 int temp;
 int i;
 int j;
 
 
 
  
     cout<<"  enter the elements in the array "<<endl;
     
     for (i=0; i<10; i++)
     {
          cout<<"  Element "<<i<<" : ";
          cin>>bubble[i];
     } 

 
 /// bubble sorting
  for (i=0; i<10; i++)
     {
          for (j=0; j<i; j++)
          {
   if(bubble[j]>bubble[i])
   ///int temp to do the swapping and store value
   
   {
   int temp=bubble[j];
   bubble[j]=bubble[i];
   bubble[i]=temp;
      }
  }
 }
  cout<<"  The sorted array is as follows : "<<endl;
     for (i=0; i<10; i++)
     {
          cout<<" \n Element "<<i<<" : "<<bubble[i];
     } 
 getch ();
 return 0;

}


No comments:

Post a Comment