Monday 25 January 2016

SEARCHING AND SORTING

Liner Search
#include<iostream.h>
#include<conio.h>
void main()
{
int a[5],i,temp=0,s;
clrscr();
cout<<"Enter array elements:\n";
for(i=0;i<5;i++)
cin>>a[i];
cout<<"Enter the element to be search:\n";
cin>>s;
for(i=0;i<5;i++)
{
            if(a[i]==s)
            {
            temp=1;
            break;
            }
}
if(temp==1)
cout<<"The element is found at location :%d\n"<<i++;
else
cout<<"The element is not found:\n";
getch();
}
Binary Search
#include<iostream.h>
#include<conio.h>
int binary(int a[],int n,int key)
{
            int left,mid,right;
            left=0;
            right=n-1;
while(left<=right)
{
            mid=(left+right)/2;
            if(key==a[mid])
            return mid;
            else if(key<a[mid])
            right=mid-1;
            else if(key>a[mid])
            left=mid+1;
}
return -1;
}
void main()
{
int a[10],n,key,pos,i;
clrscr();
cout<<"\n Enter no.of elements\n";
cin>>n;
cout<<"\n Enter elements\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\n Enter the search element\n";
cin>>key;
pos=binary(a,n,key);
if(pos>=0)
cout<<"\n The element found at pos : "<<pos;
else
cout<<"\n The element not found";
getch();
}
Bubble Sort
#include<iostream.h>
#include<conio.h>
void main()
{
int a[50],i,j,t,n;
clrscr();
cout<<"\nEnter no.of  elements in the array:\n";
cin>>n;
cout<<"Enter array elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
            for(j=i;j<n;j++)
            {
                        if(a[i]>a[j])
                        {
                        t=a[i];
                        a[i]=a[j];
                        a[j]=t;
                        }
            }
}
cout<<"The sorted array is:\n";
for(i=0;i<n;i++)
cout<<a[i]<<endl;
getch();
}

Quick Sort
#include<iostream.h>
#include<conio.h>
int a[10],l,u,i,j;
void quick(int *,int,int);
void main()
{
clrscr();
cout <<"enter 10 elements";
for(i=0;i<10;i++)
cin >> a[i];
l=0;
u=9;
quick(a,l,u);
cout <<"sorted elements";
for(i=0;i<10;i++)
cout << a[i] << " ";
getch();
}
 
void quick(int a[],int l,int u)
{
   int p,temp;
   if(l<u)
   {
   p=a[l];
   i=l;
   j=u;
    while(i<j)
   {
      while(a[i] <= p && i<j )
                i++;
      while(a[j]>p && i<=j )
                  j--;
      if(i<=j)
      {
      temp=a[i];
      a[i]=a[j];
      a[j]=temp;}
  }
  temp=a[j];
  a[j]=a[l];
  a[l]=temp;
  cout <<"\n";
  for(i=0;i<10;i++)
  cout <<a[i]<<" ";
  quick(a,l,j-1);
  quick(a,j+1,u); 
 }
}
 
Selection Sort
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[20],i,n,j,k,min,loc,temp;
cout<<"Enter no.of elements\n";
cin>>n;
cout<<"\n Enter elements=\n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
for(j=1;j<=n-1;j++)
{
min=a[j];
loc=j;
            for(k=j+1;k<=n;k++)
            {
            if(min>a[k])
            {
            min=a[k];
            loc=k;
            }
            }
temp=a[j];
a[j]=a[loc];
a[loc]=temp;
}
cout<<"\n After sorting :\n";
for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}
getch();
}
Insertion Sort
#include<iostream.h>                    
#include<conio.h>
void main()
{
clrscr();
int a[10],i,j,k,temp;
cout<<"\n Enter elements\n";
for(i=0;i<=10;i++)
{
cin>>a[i];
}
for(i=1;i<=10;i++)
{

            for(j=i;j>=1;j--)
            {
              if(a[j]<a[j-1])
              {
              temp=a[j];
              a[j]=a[j-1];
              a[j-1]=temp;
              }
              else
              break;
            }
}
cout<<"\n After sorting :\n";
for(k=0;k<=10;k++)
{
cout<<a[k]<<endl;
}
getch();
}
Merge Sort
#include <iostream.h>
#include <conio.h>
void merge(int *,int, int , int );
void mergesort(int *a, int low, int high)
{
    int mid;
    if (low < high)
    {
            mid=(low+high)/2;
            mergesort(a,low,mid);
            mergesort(a,mid+1,high);
            merge(a,low,high,mid);
    }

}
void merge(int *a, int low, int high, int mid)
{
    int i, j, k, c[50];
    i = low;
    k = low;
    j = mid + 1;
    while (i <= mid && j <= high)
    {
            if (a[i] < a[j])
            {
                c[k] = a[i];
                k++;
                i++;
            }
            else
            {
                c[k] = a[j];
                k++;
                j++;
            }
    }
    while (i <= mid)
    {
            c[k] = a[i];
            k++;
            i++;
    }
    while (j <= high)
    {
            c[k] = a[j];
            k++;
            j++;
    }
    for (i = low; i < k; i++)
    {
            a[i] = c[i];
    }
}
int main()
{
    int a[20], i;
    clrscr();
    cout<<"enter  the elements\n";
    for (i = 0; i < 5; i++)
    {
            cin>>a[i];
    }
    mergesort(a, 0, 4);
    cout<<"sorted array\n";
    for (i = 0; i < 5; i++)
    {
            cout<<a[i]<<endl;
    }
    getch();
    return 0;
}





Heap Sort
#include<iostream.h>
#include<conio.h>
#define MAX 20
void heap_sort(int a[],int n)
{
int i,temp;
            for(i=2;i<=n;i++)
            {
                        if(a[i]>a[i/2])
                        {
                        temp=a[i];
                        a[i]=a[i/2];
                        a[i/2]=temp;
                        if(i/2>1)
                        heap_sort(a,i/2);
                        }
            }
}
void main()
{
int a[MAX],t,i,n;
clrscr();
cout<<"Enter no.of elements\n";
cin>>n;
cout<<"Enter the elements\n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
for(i=n;i>0;i--)
{
            heap_sort(a,i);
            t=a[i];
            a[i]=a[1];
            a[1]=t;
}
cout<<"Elements after heap sort\n";
for(i=1;i<=n;i++)
cout<<a[i]<<endl;
getch();

}

Wednesday 20 January 2016

LINKED LIST PROGRAM

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
    int data;
    node *link;
};
class linkedlist
{
    node *start;
    public:
    linkedlist()
    {
start=NULL;
    }
    void insertbeg(int x);
    void insertend(int x);
    void insertpos(int x);
    void delbeg();
    void delend();
    void delpos();
    void display();
};
void linkedlist::insertbeg(int x)
{
   node *nnode=new node;
   nnode->data=x;
   nnode->link=start;
   start=nnode;
}
void linkedlist::insertend(int x)
{
node *nnode=new node;
nnode->data=x;
nnode->link=NULL;
node *temp=start;
while(temp->link!=NULL)
   temp=temp->link;
temp->link=nnode;
}
void linkedlist::insertpos(int x)
{
    int pos;
    cout<<"Enter position";
    cin>>pos;
    int c=1;
    node *nnode=new node;
    nnode->data=x;
    node *temp=start;
    node *prev;
    while(c<pos)
    {
prev=temp;
temp=temp->link;
c=c+1;
    }
    prev->link=nnode;
    nnode->link=temp;
}
void linkedlist::delbeg()
{
   node *temp=start;
   start=start->link;
   cout<<"Deleted element is "<<temp->data<<endl;
   delete temp;
   getch();
}
void linkedlist::delend()
{
   node *temp=start;
   node *prev;
   while(temp->link!=NULL)
   {
      prev=temp;
      temp=temp->link;
   }
   prev->link=NULL;
   cout<<"Deleted"<<temp->data<<endl;
   delete temp;
   getch();
}
void linkedlist::delpos()
{
    int pos;
    cout<<"Enter position";
    cin>>pos;
    int c=1;
    node *temp=start;
    node *prev;
    while(c<pos)
    {
      prev=temp;
      temp=temp->link;
      c=c+1;
    }
    prev->link=temp->link;
    cout<<"Deleted"<<temp->data<<endl;
    delete temp;
    getch();
}
void linkedlist::display()
{
  node *temp=start;
  while(temp!=NULL)
  {
     cout<<temp->data<<"->";
     temp=temp->link;
  }
  cout<<endl;
  getch();
}
void main()
{
    clrscr();
    int op;
    linkedlist l;
    do
    {
clrscr();
cout<<"1.insert begin"<<endl;
cout<<"2.insert end"<<endl;
cout<<"3.insert position"<<endl;
cout<<"4.delete begin"<<endl;
cout<<"5.delete end"<<endl;
cout<<"6.delete position"<<endl;
cout<<"7.display"<<endl;
cout<<"8.exit"<<endl;
cout<<"enter ur option:";
cin>>op;
switch(op)
{
   case 1:int n;
  cout<<"Enter element to insert:";
  cin>>n;
  l.insertbeg(n);
  break;
   case 2:cout<<"enter element to insert:";
  cin>>n;
  l.insertend(n);
  break;
   case 3:cout<<"Enter element to insert:";
  cin>>n;
  l.insertpos(n);
  break;
   case 4:l.delbeg();
  break;
   case 5:l.delend();
   break;
   case 6:l.delpos();
   break;
   case 7:l.display();
   break;
   case 8:exit(0);
   default:
     cout<<"Invalid option"<<endl;
     break;
   }
   }while(op!=8);
   }




Monday 11 January 2016

ADD TWO MATRIX BY USING ARRAYS



. C program to accept two square matrix and find sum of two matrices.
#include<stdio.h>
#include<conio.h>
void  main()
{
  int a[2][2],b[2][2],c[2][2],i,j;
  clrscr();
  printf("Enter the First matrix->\n");
  for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
            scanf("%d",&a[i][j]);
            }
}
  printf("\nEnter the Second matrix->");
  for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
            scanf("%d",&b[i][j]);
            }
}
  printf("\nThe First matrix is\n");
 for(i=0;i<2;i++)
{
      printf("\n");
            for(j=0;j<2;j++)
                        {
            printf("%d\t",a[i][j]);
            }
  }
  printf("\nThe Second matrix is\n");
for(i=0;i<2;i++)
{
      printf("\n");
            for(j=0;j<2;j++)
                        {
            printf("%d\t",b[i][j]);
            }
  }
   for(i=0;i<2;i++)
{
                 for(j=0;j<2;j++)
                 {
                c[i][j]=a[i][j]+b[i][j];
                  }
}
   printf("\nThe Addition of two matrix is\n");
   for(i=0;i<2;i++)
   {
       printf("\n");
       {
       for(j=0;j<2;j++)
       printf("%d\t",c[i][j]);
       }
}
   getch();
  }
INPUT:
Enter the First matrix->
2
2
3
4
Enter the Second matrix->
1
2
3
4
OUTPUT:
The First matrix is
2       2
3       4
The Second matrix is
1       2
3       4
The Addition of two matrix is
3       4
6       8