Saturday, August 25, 2007

Searching Method in C - Linear Search/Binary Search/Depth first search

Searching Method in C - Linear Search/Binary Search/Depth first search
==========================================================

Linear Search
=============


#include <stdio.h>
#include <conio.h>
void main ()
{
int array[20], n, sea_no, i, j;
int create (int []);
int linear_search (int [], int, int);
clrscr ();
n = create (array);
printf("\nEnter the number you want to search: ");
scanf ("%d",&sea_no);
clrscr ();
i = linear_search (array, n, sea_no);
if (i == 0)
printf ("\nSearch value does not exist");
else
printf("\nThe search value is at location %d\n",i);
printf("\nThe array is as follows:\n");
for (j = 0 ; j < n ; j++)
printf("%d ",array[j]);
getch ();
}

int create (int a[20])
{
int n, i;
printf("\nEnter how many numbers you want to enter(Enter less than 20): ");
scanf("%d",&n);
printf("\nEnter the numbers:\n");
for (i = 0 ; i < n ; i++)
scanf("%d",&a[i]);
return (n);
}

int linear_search (int a[], int n, int s)
{
int i;
for (i = 0 ; i < n ; i++)
{
if(s == a[i])
{
printf("\nSEARCH VALUE FOUND\n");
return (i+1);
}
}
return (0);
}


Binary Search
==============


#include <stdio.h>
#include <conio.h>

void main ()
{
int a[20], n, sea_no;
int create (int []);
void binary_search (int [], int, int);
clrscr ();
n = create(a);
printf("\nEnter the number you want to search: ");
scanf("%d",&sea_no);
clrscr ();
binary_search (a,n,sea_no);
getch ();
}

int create (int a[20])
{
int n, i;
do
{
printf("\nEnter how many numbers you want to enter(Enter less than 20): ");
scanf("%d",&n);
} while (n > 20);
if (n == 0)
{
printf("Invalid number, Press any key to exit\n");
getch ();
printf("Termenating the Program.....");
delay (1500);
exit (1);
}
printf("\nEnter the numbers in ascending order:\n");
for (i = 0 ; i < n ; i++)
scanf("%d",&a[i]);
return (n);
}

void binary_search (int a[], int n, int s)
{
int lb = 0,ub = n-1,mid, flag = 1, i;
while (lb <= ub)
{
mid = (lb + ub)/2;
if (s < a[mid])
ub = mid - 1;
if (s > a[mid])
lb = mid + 1;
if (s == a[mid])
{
flag = 0;
printf("\nSEARCH FOUND\n");
printf("\nThe number you want to search is at location %d\n",mid + 1);
printf("\nThe elements are as follows:\n");
for (i = 0 ; i < n ; i++)
printf("%d ",a[i]);
break;
}
}
if (flag == 1)
{
printf("\nSearch element does not found");
}
}


Depth first search
===============



#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>

#define size 20
#define T 1
#define F 0

struct Edge
{
int terminal;
struct Edge *next;
};
struct Vertex
{
int visit;
int vertexno;
char info;
int path_length;
struct Edge *edgeptr;
};
void table(int vertexno,int matrix[size][size],struct Vertex vert[size]);
struct Edge *insert_vertex(int vertexno,struct Edge * first);
void dfs(int index,int *dist,struct Vertex vert[size]);
void output(int number,int a[size][size]);
void input(int number,int a[size][size]);

struct Edge *insert_vertex(int vertexno, struct Edge *first)
{
struct Edge * newl,*current;
newl = (struct Edge *)malloc(sizeof(struct Edge));
newl->terminal = vertexno;
newl->next = NULL;
if(first)
return(newl);
for(current = first; current->next;current=current->next)
current->next = newl;
return(first);
}
void table(int vertexno,int matrix[size][size],struct Vertex vert[size])
{
int i,j;
for(i=0;i<vertexno;i++)
{
vert[i].visit = F;
vert[i].vertexno = i+1;
vert[i].info = 'A' + i;
vert[i].edgeptr = NULL;
}
for(i=0;i<vertexno;i++)
for(j=0;j<vertexno;j++)
if(matrix[i][j] > 0)
vert[i].edgeptr = insert_vertex(j,vert[i].edgeptr);
}
void dfs(int index,int *dist,struct Vertex vert[size])
{
struct Edge *link;
vert[index].visit = T;
vert[index].path_length = *dist;
*dist += 1;
for(link = vert[index].edgeptr;link;link = link->next)
if(vert[link->terminal].visit == F)
dfs(link->terminal,dist,vert);
}
void input(int number,int a[size][size])
{
int i,j;
printf("\n Input the adjacency matrix \n");
for(i=0;i<number;i++)
{
for(j=0;j<number;j++)
{
scanf("%d",&a[i][j]);
}
printf("\n");
}
}
void output(int number,int a[size][size])
{
int i,j;
printf("\n Input the adjacency matrix \n");
for(i=0;i<number;i++)
{
for(j=0;j<number;j++)
{
printf("%d",&a[i][j]);
}
printf("\n");
}
}

void main()
{
int i;
int number,index,dist;
int a[size][size];
struct Vertex vert[size];
struct Edge *list;
clrscr();
printf("\nInput the number of vertices in the graph : ");
scanf("%d",&number);
input(number,a);
output(number,a);
table(number,a,vert);
printf("\nInput the starting vertex %d : ",number-1);
scanf("%d",&index);
dist = 0;
dfs(index,&dist,vert);
printf("\nPath length of the vertex from %c ",vert[index].info);
printf("\nVertex Length Vertex Connectivity : \n ");
for(i=0;i<number;i++)
{
printf("\n %c %d",vert[i].info,vert[i].path_length);
for(list=vert[i].edgeptr;list;list=list->next)
{
printf(" ");
putchar(list->terminal+'A');
}
}
getch();
}

20 comments:

Anonymous said...

This is my first time go to see at here and i
am truly pleassant to read all at one place.

Take a look at my weblog; Premium Cardsharing

Anonymous said...

Ahaa, its fastidious dialogue concerning this post at this
place at this blog, I have read all that, so at this time me also commenting at this place.


Feel free to visit my website; cardshare

Anonymous said...

For example, if you sell paintings a link from an art school or a gallery would
be important. Long tail searches are combined searches formed from several high frequency keywords and key phrases.

In many cases, getting ranked for specific local terms is easier than "taking candy from babies.

my blog post www.ninhao.com

Anonymous said...

I love it when pеоρle comе tοgetheг and ѕharе οpinions.

Gгeat ѕіte, ѕticκ with іt!


Hеre iѕ my websitе; juicing for cancer treatment

Anonymous said...

Τhis post is genuinely a good one it аssists new web ѵіewers, ωho are
ωishing for blоgging.

Alѕο visit my blog ρost :: vegetarian pizza recipes

Anonymous said...

Wгite more, thats all I havе to say.
Literally, it seems as though you relied οn thе video to make your point.
You obviouslу knоw whаt уouгe talκing about, why wаste your intelligеnce
on just рoѕtіng videos to your ωeblog ωhen
уou сould bе giving us somethіng enlіghtening tο read?


Hегe is mу web site dailystrength.org

Anonymous said...

What i don't understood is in reality how you are no longer actually a lot more smartly-appreciated than you might be now. You are so intelligent. You know therefore considerably in terms of this subject, produced me personally imagine it from so many various angles. Its like men and women aren't inteгested unlesѕ it iѕ one thing to асcompliѕh with Woman gaga!
Yоur own stuffs excellent. Αll the
time take carе of it up!

Rеview my wеb blog http://www.veggiejuicerecipes.com/

Anonymous said...

уou're in point of fact a just right webmaster. The website loading pace is incredible. It kind of feels that you'гe ԁoing
аny unique trіcκ. Moгеоνer,
The contents are masteгwork. you've done a great job in this topic!

Also visit my web-site ... http://www.vegetablejuicingrecipesforweightloss.com

Anonymous said...

Hello! Do you know if they maκe any plugins to helр wіth SEO?

I'm trying to get my blog to rank for some targeted keywords but I'm nоt
seeing veгy good rеsults. If you knοw of
any please share. Αppгeсiate іt!

my web-ѕite :: juicing for beauty

Anonymous said...

What's up to every one, the contents existing at this web page are really amazing for people experience, well, keep up the nice work fellows.

Here is my blog post net.pl

Anonymous said...

Hey there fantastic website! Does running a blog like this require a massive amount
work? I've no understanding of programming but I had been hoping to start my own blog soon. Anyhow, if you have any suggestions or techniques for new blog owners please share. I know this is off subject however I simply needed to ask. Thank you!

my blog ... http://martial-arts-radio.net/wire-high-definition-digital-recording-device-recorders.htm

Anonymous said...

Its not my first time to go to see this web
page, i am browsing this site dailly and obtain pleasant facts from here every day.


my web site ... teeth bleaching kits

Anonymous said...

I am really impressed with your writing skills and also with the
layout on your weblog. Is this a paid theme or did you modify it
yourself? Anyway keep up the excellent quality writing, it is rare to see a
nice blog like this one these days.

Feel free to surf to my web blog ... Www.Ecoidea.In

Anonymous said...

Hello, I check your new stuff on a regular basis.
Your story-telling style is awesome, keep doing what you're doing!

my site; Cosmetic Dentist Glasgow

Anonymous said...

Veгу nicе post. I cеrtaіnly аpрrecіate thіs website.
Ѕtick ωith it!

Мy ѕite Vіtаmіn с facts :
: http://mediabass.Tv/ ::

Anonymous said...

At this moment I am going to do my breakfast, once having my breakfast coming again
to read other news.

Here is my homepage; lose weight quick

Anonymous said...

I think the admin of this website is in fact working hard in support
of his web site, because here every stuff is quality based stuff.


My website ... diagnostic medical ()

Anonymous said...

Greetings! I've been following your web site for some time
now and finally got the courage to go ahead and give you a shout out from Dallas Tx!
Just wanted to mention keep up the great work!


my web-site Christian Louboutin Discount

Anonymous said...

Good day! I simply want to offer you a huge thumbs up for your excellent information you have
here on this post. I will be returning to your site for more
soon.

Feel free to surf to my page :: Christian Louboutin Sale

Anonymous said...

Hi, I think your site might be having browser compatibility issues.
When I look at your website in Ie, it looks fine but when opening in Internet Explorer, it has some
overlapping. I just wanted to give you a quick heads up!
Other then that, terrific blog!

Also visit my webpage; small business pbx systems New York City