Wednesday, October 15, 2014

Implement Insertion Sort Algorithms and test it with input of array of length 15 {9,13,15,11,14,1,8, 6,2,7,4,5,10,3,12}.

Implement Insertion Sort Algorithms and test it with input of array of length 15 {9,13,15,11,14,1,8, 6,2,7,4,5,10,3,12}.

CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Labtask4_1
{
    public class Insertion_Sort
    {
        int[] A = { 9, 13, 15, 11, 14, 1, 8, 6, 2, 7, 4, 5, 10, 3, 12 };
        public void Show()
        {
            for (int i = 0; i < 15; i++)
            {
                Console.Write("{0},", A[i]);
            }
            Console.WriteLine();
        }
        public void Sort()
        {
            for (int i = 0; i < 15; i++)
            {
                int num = A[i];
                int j = i - 1;
                while (j >= 0 && A[j] > num)
                {
                    A[j + 1] = A[j];
                    j--;
                }
                A[j + 1] = num;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Insertion_Sort A = new Insertion_Sort();
            A.Show();
            Console.WriteLine("*****AFTER SORTING THE ARRAY*****\n");
            A.Sort();
            A.Show();
            Console.ReadLine();
        }
    }

}

Wednesday, September 24, 2014

Take 10 inputs from the user and assign them into two arrays (make 2 arrays of 5 lengths each), merge those arrays and obtain the result in the sorted manner.

Objective:
Take 10 inputs from the user and assign them into two arrays (make 2 arrays of 5 lengths each), merge those arrays and obtain the result in the sorted manner.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Merg
{
    class Program
    {
        static void Main(string[] args)
        {

            int[] Ary1 = new int[5];
            Console.WriteLine("Enter the 5 values of First Array array");
            for (int i = 0; i < 5; i++)
            {
                Ary1[i] = int.Parse(Console.ReadLine());
            }
            int[] Ary2 = new int[5];
            Console.WriteLine("Enter the 5 values of Second array");
            for (int i = 0; i < 5; i++)
            {
                Ary2[i] = int.Parse(Console.ReadLine());
            }
            int[] Ary = new int[Ary1.Length + Ary2.Length];
            Ary1.CopyTo(Ary, 0);
            Ary2.CopyTo(Ary, Ary1.Length);
            for (int i = 0; i < 9; i++)
            {
                int min = i;
                for (int j = i + 1; j < 10; j++)
                {
                    if (Ary[j] < Ary[min])
                    {
                        min = j;
                    }
                }
            }
            Console.WriteLine("Sorting values by using merg sort");
            for (int i = 0; i < 10; i++)
            {
                Console.Write("{0}",Ary[i]);
                Console.Write(" ");
            }
        }
    }
}

Output:





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:



Wednesday, September 17, 2014

Objective:
Write a program to find.
XA + YB
A. B
Test the program using A= (16, -6, 7), B= (4, 2,-3), X= 2, Y= -5



#include<iostream>
using namespace std;
class Vectors
{
public:
       void GetVectorAB(int x, int y, int A[3], int B[3])
       {
              int XA[3];
              int YB[3];
              int ANS[3]={0};
       int i;
       for(i=0; i<3; i++)
       {
       XA[i]=x*A[i];
       }
       for(i=0; i<3; i++)
       {
       YB[i]=y*B[i];
       }
       for(i=0; i<3; i++)
       {
       ANS[i]=XA[i]+YB[i];
       }

       cout<<"XA+YB=(";
       for(i=0; i<3; i++)
       {
              cout<<ANS[i]<<" ";
       }
       cout<<")"<<endl;
       }
      

       void GetVectorAdotB(int A[3], int B[3])
       {
       int dot[3];
       int ab=0;
       int i;
       for(i=0; i<3; i++)
       {
       dot[i]=A[i]*B[i];
       }

       for(i=0; i<3; i++)
       {
              ab=ab+dot[i];
       }

       cout<<"A.B = "<<ab<<endl;
      
       }
};

void main()
{
       int n;
       Abc:
cout<<"What expression you want to do???"<<endl;
cout<<"1:     XA+YB???"<<endl;
cout<<"2:     A.B????"<<endl;
cin>>n;

if(n==1)
{
       int a,b;
       int A[3]={0};
       int B[3]={0};
       cout<<"Enter vector A"<<endl;
       for(int i=0; i<3; i++)
       {
       cin>>A[i];
       }

       cout<<"Enter vector B"<<endl;
       for(int i=0; i<3; i++)
       {
       cin>>B[i];
       }
       cout<<"Enter a constant value for X"<<endl;
       cin>>a;
       cout<<"Enter a constant value for Y"<<endl;
       cin>>b;
       Vectors v1;
       v1.GetVectorAB(a,b,A,B);
}
else
       if(n==2)
       {
              int A[3];
              int B[3];
              cout<<"Enter vector A"<<endl;
              for(int i=0; i<3; i++)
              {
              cin>>A[i];
              }
              cout<<"Enter vector B"<<endl;
              for(int i=0; i<3; i++)
              {
              cin>>B[i];
              }
              Vectors v2;
              v2.GetVectorAdotB(A,B);
       }
       else
       {
       cout<<"Enter a valid number\n\n";
       goto Abc;
       }
}