Comments

Tuesday, August 27, 2013

Bubble sort using pointers.

Posted by at 2:08 AM Read our previous post


Object:

Bubble sort using pointers.
Source Code:
#include <iostream>
using namespace std;
void bubble(int*,int);//////////////////prototype

void main()
{
       int Array[5],size=5,*pointer;

       for(int num=0;num<size;num++)
       {
       cout<<"Enter element no : "<<num+1<<endl;
       cin>>Array[num];
       }
       pointer=Array;
       bubble(pointer,size);
       }

       void bubble(int *pointer,int size)
       {
              int count1,count2,swap;

              for(count1=0;count1<size-1;count1++)
              {
                     for(count2=0;count2<size-count1-1;count2++)
                     {
                    
                           if(*(pointer+count2)>*(pointer+count2+1))
                           {
                           swap=*(pointer+count2);
                           *(pointer+count2)=*(pointer+count2+1);
                           *(pointer+count2+1)=swap;
                          
                           }
                    
                     }
              }

              for(count1=0;count1<size;count1++)
              {
              cout<<*(pointer+count1)<<" , ";
              }
       };

Output:

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Popular Posts

Labels

© Codepirate is powered by Blogger - Template designed by Stramaxon - Best SEO Template