Program for binary search, Data Structure & Algorithms

Assignment Help:

Illustrates the program for Binary Search.

Program: Binary Search

/*Header Files*/

#include

#include

/*Functions*/

void binary_search(int array[ ], int value, int size)

{

int found=0;

int low=0, high=size-1,  mid;

mid = (high+low)/2;

printf("\n\n Searching for %d\n", value);

while((!found)&&(high>=low))

{

printf("Low %d Mid%d High%d\n", low, mid, high);

if(value==array[mid] )

{

printf("Key value searched out at position %d",mid+1);

found=1;

}

else

{if (value

high = mid-1;

else

 low = mid+1;

mid = (high+low)/2;

}

}

if (found==1

printf("Search successful");

else

printf("Key value not found");

}

/*Main Function*/

void main(void)

{

int array[100], i;

/*Inputting Values to Array*/

for(i=0;i<100;i++)

{ printf("Enter the name:");

scanf("%d", array[i]);

}

printf("Result of search %d\n", binary_searchy(array,33,100));

printf("Result of search %d\n", binary_searchy(array, 75,100));

printf("Result of search %d\n", binary_searchy(array,1,100));

}


Related Discussions:- Program for binary search

Methods of collision resolution, Methods of Collision Resolution 1)  Co...

Methods of Collision Resolution 1)  Collision Resolution by separate chaining  2)  Collision Resolution by open addressing

Queue, what''s queue ?

what''s queue ?

Algorithm to insert element to a max-heap sequentially, Q. Write  down the ...

Q. Write  down the  algorithm  to  insert  an  element  to  a  max-heap  which  is  represented sequentially.           Ans: The algorithm to insert an element "newkey" to

Full binary trees, Full Binary Trees: A binary tree of height h that had 2...

Full Binary Trees: A binary tree of height h that had 2h -1 elements is called a Full Binary Tree. Complete Binary Trees: A binary tree whereby if the height is d, and all of

Write an algorithm of value in tax using pseudocode, A town contains a tota...

A town contains a total of 5000 houses. Every house owner has to pay tax based on value of the house. Houses over $200 000 pay 2% of their value in tax, houses over $100 000 pay 1.

Time complexity, The  total  of  time  needed  by  an algorithm to run to i...

The  total  of  time  needed  by  an algorithm to run to its completion is termed as time complexity. The asymptotic running time of an algorithm is given in terms of functions. Th

Linked lists, what are grounded header linked lists?

what are grounded header linked lists?

Dgsd, Ask question #sdgsdgsdginimum 100 words accepted#

Ask question #sdgsdgsdginimum 100 words accepted#

Difference in grounded header and circular header Link List, Q. State the d...

Q. State the difference between a grounded header link list and a circular header link list?     Ans: A header linked list is a linked list which all the time c

Preorder traversal of a binary tree, Preorder traversal of a binary tree ...

Preorder traversal of a binary tree struct NODE { struct NODE *left; int value;     /* can take any data type */ struct NODE *right; };   preorder(struct N

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd