Monday 10 March 2014

BINARY SEARCH

DESCRIPTION:-

A Binary search algorithm finds the position of a specified input value within an array sorted by key value. In each step, the algorithm compares the search key value with the key value at the middle value . If the keys matches , then the search key is found and its position is returned. Otherwise, if the search key is less than the middle element's key, then the algorithm repeats its action on the sub array to the left of the middle element or, if the search key is greater, on the sub-array to the right, If the remaining array to be searched is empty, then the key cannot be found in the array and  a special "not found" indication is retuned.

OPERATION:-

Binary_Search(int str[5], int element)
this function has array and element to be searched as its parameters. This function performs the binary search to find the position of element.

ALGORITHM:-

Binary_Search(int str[5], int element)
1) low <--- 0
2) high<---max-1
3) found<---0
4) repeat till low<=high
5) mid<---(low + high)/2
6) if str[mid]=element
7) then found<---1 and break
8) if str[mid]>element
9) then high=mid-1
10)else low=mid+1;
11)if found=1
12)then  print element found at position mid+1
13)else print element not found
14)end

PROGRAM CODE:-

#include<stdio.h>
#include<conio.h>
Binary_Search(int str[5], int element)
{
int low=0;
int high=4;
int mid;
int found=0;
while(low<=high)
{
mid=(low+ high)/2;
if(str[mid]==element)
{
found=1;
break;
}
if(str[mid]>element)
high=mid-1;
else
low=mid+1;
}
if(found==1)
printf("\n Element is found at position : %d", mid+1);
else
printf("\n Element is not found");
}
void main()
{
int str[5],i;
int element=0;
clrscr();
printf("Enter 5 elements :");
for(i=0;i<5;i++)
scanf("%d", &str[i]);
printf("\n Enter element to be searched : ");
Binary_Search(int str[5], int element) 
getch();
}
 

 OUTPUT:-

Enter 5 elements :
1
2
3
4
5
 Enter element to be searched : 3
 Element is found at position : 3

 

LINEAR SEARCH

DESCRIPTION:-

Linear search is a method for finding a particular value in a list, that consists of checking every one of its  elements, one at a time and in a sequence, until the desired one is found. It is special case of brute-force search. Its worst case cost is proportional to the number of elements in the list.

OPERATION:-

Linear_Search (int str[], int element)
This function takes array and element to be searched as its parameters. This function sequentially search for the element.
 

ALGORITHM:-

Linear_Search (int str[], int element)
1) i <--- 0
2) flag<---0
3) n<---number of entries in array
4) repeat till i<n
5) if str[i] = element
6) then print element found at position i+1 and set flag<---1
7) //end if
8) //end loop
9) if flag = 0
10) then print element not found
11) end
 

PROGRAM CODE:-

#include<stdio.h>
#include<conio.h>
void Linear_Search (int str[5], int element)
{
int flag=0;
int pos=0;
int i;
for(i=0;i<5;i++)
{
if(element==str[i])
{
flag=1;
pos=i+1;
printf("\n Given element is found at position : %d", pos);
}
}
if(flag==0)
{
printf("Given element is not found!!");
}
void main()
{
int str[5],i;
int element=0;
clrscr();
printf("Enter 5 elements :");
for(i=0;i<5;i++)
scanf("%d", &str[i]);
printf("\n Enter element to be searched : ");
Linear_Search (int str[], int element);
getch();
}
 

 OUTPUT:-

Enter 5 elements :
1
2
3
4
5
 Enter element to be searched: 4
 Given element is found at position : 4
 
 
 
 

Sunday 9 March 2014

FLOW CHART

CHECK WHETHER THE NUMBER IS ARMSTRONG NUMBER OR NOT?

 

 

FLOW CHART

CHECK WHETHER THE NUMBER IS PALINDROME OR NOT?

 
 
 

FLOW CHART

FLOWCHART

Design a flow chart to check that number is positive, negative or zero. If number is negative or zero then print the appropriate message. If number is positive then check number is even or odd?
 
 

Flowchart

FLOWCHART

 Factorial of a given number n

 
 


 

 

 

Saturday 12 October 2013

CIRCULAR LINK LIST

                   CIRCULAR  LINK LIST                
               INSERTION AND DELETION         

#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *link;
} *head;

void insbeg()
{
node *temp;
temp= new node;
if(temp==0)
cout<<"\nOverflow";
else
{
int data;
cout<<"Enter the data you want to insert: ";
cin>>data;
temp->data=data;
node *temp1;
if(head==0)
{
temp->link=temp;
head=temp;
}
else
{
temp->link=head;
temp1=head;
while(temp1->link!=head)
{
temp1=temp1->link;
}
head=temp;
temp1->link=head;
}
}
}
void display()
{
node *temp;
temp=head;
if(head==0)
cout<<"\nNo linked list";
else
{
cout<<"\nLinked List is : ";
while(temp->link!=head)
{
cout<<temp->data<<" ";
temp=temp->link;
}
cout<<temp->data;
}
}
void insend()
{
node *temp,*temp1;
temp=new node;
if(temp==0)
cout<<"\nOverflow";
else
{
int data;
cout<<"Enter the data you want to insert: ";
cin>>data;
temp->data=data;
if(head==0)
{
temp->link=temp;
head=temp;
}
else
{
temp->link=head;
temp1=head;
while(temp1->link!=head)
{
temp1=temp1->link;
}
temp1->link=temp;
}
}
}
void insbet()
{
node *temp,*temp1;
int count=1,tnode=1;
temp=new node;
if(temp==0)
cout<<"\nOverflow";
else
{
int data,pos;
cout<<"Enter the data you want to insert: ";
cin>>data;
cout<<"Enter the position";
cin>>pos;
temp->data=data;
temp1=head;
while(temp1->link!=head)
{
tnode++;
temp1=temp1->link;
}
if(pos==1)
{
temp->link=head;
head=temp;
temp1->link=head;
}
else
{
if(tnode<pos)
cout<<"\nPosition does not exist";
else
{
temp1=head;
while(count!=pos-1)
{
count++;
temp1=temp1->link;
}
temp->link=temp1->link;
temp1->link=temp;
}
}
}
}
void delbeg()
{
node *temp,*temp1;
if(head==0)
cout<<"\nLink List does not exist";
else
{
temp=head;
if(temp->link==head)
{
head=0;
delete temp;
}
else
{
temp1=head;
while(temp1->link!=head)
temp1=temp1->link;
head=temp->link;
temp1->link=head;
delete temp;
}
}
}
void delend()
{
node *temp,*temp1;
if(head==0)
cout<<"No Link List";
else
{
temp=head;
if(temp->link==head)
{
head=0;
delete temp;
}
else
{
while(temp->link!=head)
{
temp1=temp;
temp=temp->link;
}
temp1->link=head;
delete temp;
}
}
}
void delbet()
{
int pos;
cout<<"Enter the position";
cin>>pos;
node *temp,*temp1;
int count=1,tnode=1;
if(head==0)
cout<<"\nNo Link Iist";
else
{
temp=head;
while(temp->link!=head)
{
tnode++;
temp=temp->link;
}
if(tnode<pos)
cout<<"\nPosition does not exist";
else
{
temp1=head;
if(pos==1)
{
head=temp1->link;
temp->link=head;
delete temp1;
}
else
{
temp=head;
while(count!=pos)
{
count++;
temp1=temp;
temp=temp->link;
}
temp1->link=temp->link;
delete temp;
}
}
}
}
void main()
{
clrscr();
int ch;
char ans;
cout<<"\nMENU";
cout<<"\n1).Insert in the Begining";
cout<<"\n2).Insert in the End";
cout<<"\n3).Insert in between ";
cout<<"\n4).Delete from the Begining";
cout<<"\n5).Delete from the End";
cout<<"\n6).Delete from the between";
do
{
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:insbeg();
       display();
       break;
case 2:insend();
       display();
       break;
case 3:insbet();
       display();
       break;
case 4:delbeg();
       display();
       break;
case 5:delend();
       display();
       break;
case 6:delbet();
       display();
       break;
default :cout<<"\n Wrong choice";
}
cout<<"\nDo you want more press y for yes: ";
cin>>ans;
}while(ans=='y');
getch();
}