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();

}

No comments:

Post a Comment