Monday, September 22, 2014

: Implementation/ coding of the following algorithms/ functions regarding Arrays • Bubble Sort • Selection Sort • Insertion Sort • Mearging

 Implementation/ coding of the following algorithms/ functions regarding Arrays
  • Bubble Sort
  • Selection Sort
  • Insertion Sort
  • Mearging
Code:
#include<iostream>
using namespace std;
class Arrays
{
public:
       void BubbleSrt(int A[10])
       {     
              int i,j;
              int temp;
              for(i=0; i<10; i++)
              {
                     for(j=i+1; j<10; j++)
                     {
                           if(A[i]>A[j])
                           {
                                  temp=A[i];
                                  A[i]=A[j];
                                  A[j]=temp;
                           }
                     }
              }
              cout<<"Sorted values after bubble sort"<<endl;
              for(i=0; i<10;i++)
              {
                     cout<<A[i]<<"";
              }
       }

       void SelectionSrt(int A[10])
       {     
              int i,j;
              int temp;
              for(i=0; i<10; i++)
              {
                     for(j=i; j<10; j++)
                     {
                           if(A[i]>A[j])
                           {
                                  temp=A[i];
                                  A[i]=A[j];
                                  A[j]=temp;
                           }
                     }
              }
              cout<<"Sorted values after bubble sort"<<endl;
              for(i=0; i<10;i++)
              {
                     cout<<A[i]<<"";
              }
       }

       void InsertionSrt(int A[10])
       {     
              int i, j, key;
              for(j = 1; j < 10; j++) 
              {
                     key = A[j];
                     for(i =j-1; (i >= 0) && (A[i] < key); i--) 
                     {
                           A[i+1] = A[i];
                     }
                     A[i+1] = key; 
              }
              cout<<"Sorted values after Insertion Sort"<<endl;
              for(i=0; i<10; i++)
              {
                     cout<<A[i]<<"";
              }
       }

       void LinearSch(int A[10],int val)
       {
              int i;
              for(i=0; i<10; i++)
              {
                     if(val==A[i])
                     {
                           cout<<"Value found at index "<<i<<endl;
                     }
                     else
                     {
                           cout<<"value not found"<<endl;
                     }
              }
       }

       void BinarySch(int A[10],int val)
       {     
              int start=0;
              int end=9;
              int mid=0;
              int loc=-1;
              while(start<=end)
              {
                     mid=(start+end)/2;
                     if(val==A[mid])
                     {
                           loc=mid;
                           break;
                     }
                     else
                     {
                           if(val<A[mid])
                           {
                                  end=mid-1;
                           }
                           else
                           {
                                  start=mid+1;
                           }
                     }
              }
              if(loc==-1)
              {
                     cout<<"Value not found in array";
              }
              else
              {
                     cout<<val<<"  found at index "<<loc<<endl;
              }
       }
};

void main()
{
       int i;
       int n;
       int A[10];
Abc:
       cout<<endl<<endl;
       cout<<".......*********........"<<endl;
       cout<<"What do you want to do???"<<endl;
       cout<<"[1]  Sorting"<<endl;
       cout<<"[2]  Searching"<<endl;
       cout<<"[0]  Exit"<<endl;
       cin>>n;

       switch(n)
       {
       case 1:
              {
                     int e;
                     cout<<"[1]  Bubble sort"<<endl;
                     cout<<"[2]  Selection Sort"<<endl;
                     cout<<"[3]  Insertion Sort"<<endl;
                     cout<<"[0]  Exit"<<endl;
                     cin>>e;
                     switch(e)
                     {
                     case 1:
                           {
                                  cout<<"Enter Values in array";
                                  for(i=0; i<10; i++)
                                  {
                                         cin>>A[i];
                                  }
                                  Arrays A1;
                                  A1.BubbleSrt(A);
                                  goto Abc;
                                  break;
                           }
                     case 2:
                           {
                                  cout<<"Enter Values in array";
                                  for(i=0; i<10; i++)
                                  {
                                         cin>>A[i];
                                  }
                                  Arrays A1;
                                  A1.SelectionSrt(A);
                                  goto Abc;
                                  break;
                           }

                     case 3:
                           {
                                  cout<<"Enter Values in array";
                                  for(i=0; i<10; i++)
                                  {
                                         cin>>A[i];
                                  }
                                  Arrays A1;
                                  A1.InsertionSrt(A);
                                  goto Abc;
                                  break;
                           }
                     case 0:
                           {
                                  break;
                           }
                     }
              }
       case 2:
              {
                     int e;
                     int v;
                     cout<<"[1]  Linear Search"<<endl;
                     cout<<"[2]  Binary Search"<<endl;
                     cout<<"[0]  Exit"<<endl;
                     cin>>e;

                     switch(e)
                     {
                     case 1:
                           {
                                  cout<<"Enter Values in array"<<endl;
                                  for(i=0; i<10; i++)
                                  {
                                         cin>>A[i];
                                  }
                                  cout<<"Enter value you want to search"<<endl;
                                  cin>>v;
                                  Arrays A1;
                                  A1.LinearSch(A, v);
                                  goto Abc;
                                  break;
                           }
                     case 2:
                           {
                                  cout<<"Enter Values in array"<<endl;
                                  for(i=0; i<10; i++)
                                  {
                                         cin>>A[i];
                                  }
                                  cout<<"Enter value you want to search"<<endl;
                                  cin>>v;
                                  Arrays A1;
                                  A1.BinarySch(A, v);
                                  goto Abc;
                                  break;
                           }
                     case 0:
                           {
                                  break;
                           }

                     }
              }

       }
}



Output:



No comments:

Post a Comment