XII-COMPUTER SCIENCE--DATA STRUCTURE POGRAMS-2013-14 (BINARY SEARCH, INSERTION,SELECTION SORT)

//PROG-1  INSERTION SORT
#include<conio.h>
#include<iostream.h>
void main()
{
int a[100];
int i,j,n,temp;
clrscr();
cout<<"\n                     INSERTION SORT  \n";
cout<<"   ENTER TOTAL NO. OF ELEMENTS ( N<=100 )TO BE SORTED :   ";
cin>>n;
cout<<"   NOW START DATA ENTRY...  \n";
for(j=0;j<n;j++)
{
  cout<<"\n Enter element no. "<<(j+1)<<'\t';
  cin>>a[j];
}
for(i=0;i<n-1;i++)
{     temp=a[i+1];
       j=i;
      while((a[j]>temp)&&(j>=0))
               {
                   a[j+1]=a[j];
                   j--;
               }
      a[j+1]=temp;
}
cout<<"\n   RESULT :  ARRAY AFTER SORTING IS  \n";
for(j=0;j<n;j++)
cout<<a[j]<<"   ";
cout<<endl;
getch();
}
-----------------------------------------------------------------
//PROG - 2  SELECTION SORT
#include<conio.h>
#include<iostream.h>
void main()
{
int a[50];
int i,j,n,smallest,temp;
//clrscr(); 
cout<<"\n                     SELECTION SORT  \n";
cout<<"   ENTER TOTAL NO. OF ELEMENTS ( N<=50 )TO BE SORTED :   ";
cin>>n;
cout<<"   NOW START DATA ENTRY...  \n";
for(j=0;j<n;j++)
  {
      cout<<"\n Enter element no. "<<(j+1)<<'\t';
      cin>>a[j];
  }
for(i=0;i<n-1;i++)
{
   smallest=i;
   for(j=i+1;j<n;j++)
                 if(a[smallest]>a[j])
                             smallest=j;
   temp=a[i];
   a[i]=a[smallest] ;
   a[smallest]=temp;
 }
cout<<"\n RESULT :  ARRAY AFTER SORTING IS  \n";
for(j=0;j<n;j++)
            cout<<a[j]<<"   ";
cout<<endl;
getch();
}
-------------------------------------------------------------------------------------
//PROG-3  BINARY SEARCH--When input is in ASCENDING ORDER

#include<conio.h>
#include<iostream.h>
void main()
{
int a[50];
int j,n,data,found,first,mid,last;
//clrscr(); 
cout<<"\n                 BINARY SEARCH  \n";
cout<<"   ENTER TOTAL NO.OF ELEMENTS ( N<=50 )PRESENT IN THE ARRAY (IN ASCENDING ORDER) :   ";
cin>>n;
for(j=0;j<n;j++)
  {
      cout<<"\n Enter (In Ascending Order ) element no. "<<(j+1)<<'\t';
      cin>>a[j];
  }
  cout<<"   NOW ENTER A DATA TO BE SEARCHED...  \n";
  cin>>data;
  first=0; last=n-1; found=0;
  while(first<=last)
  {
    mid=(first+last)/2;
    if(data==a[mid]) {found=1;break;}
    if(data<a[mid]) last=mid-1;
      else first=mid+1;
   }
   if(found) cout<<"  ELEMENT FOUND AT POSITION   "<<(mid+1);
      else  cout<<"   ELEMENT NOT FOUND";
cout<<endl;
getch();
}
...................................................................................
//PROG-4  BINARY SEARCH--When input is in DESCENDING ORDER
#include<conio.h>
#include<iostream.h>
void main()
{
int a[50];
int j,n,data,found,first,mid,last;
//clrscr(); 
cout<<"\n                 BINARY SEARCH  \n";
cout<<"   ENTER TOTAL NO.OF ELEMENTS ( N<=50 )PRESENT IN THE ARRAY (IN DESCENDING ORDER) :   ";
cin>>n;
for(j=0;j<n;j++)
  {
      cout<<"\n Enter (In Descending Order ) element no. "<<(j+1)<<'\t';
      cin>>a[j];
  }
  cout<<"   NOW ENTER A DATA TO BE SEARCHED...  \n";
  cin>>data;
  first=0; last=n-1; found=0;
  while(first<=last)
  {
    mid=(first+last)/2;
    if(data==a[mid]) {found=1;break;}
    if(data<a[mid]) first=mid+1;
      else last=mid-1 ;
   }
   if(found) cout<<"  ELEMENT FOUND AT POSITION   "<<(mid+1);
      else  cout<<"   ELEMENT NOT FOUND";
cout<<endl;
getch();
}
 

Popular posts from this blog

XII- CS-- SOLVED SQL queries (SQL PRACTICAL ASSIGNMENS)

SQL--HOME ASSIGNMENTS(XII Class)

Python-MySQL Connectivity