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

Array and two-dimensional array, Q. Describe the term array.  How do we rep...

Q. Describe the term array.  How do we represent two-dimensional arrays in memory?  Explain how we calculate the address of an element in a two dimensional array.

Interest rate, explain the determination of interest rate in the classical ...

explain the determination of interest rate in the classical system.

Abstract data type- queue, A significant aspect of Abstract Data Types is t...

A significant aspect of Abstract Data Types is that they explain the properties of a data structure without specifying the details of its implementation. The properties might be im

Finite automata, find the grammar of regular expression of (a/?)(a/b)?

find the grammar of regular expression of (a/?)(a/b)?

Threaded Binary Tree, If a node in a binary tree is not containing left or ...

If a node in a binary tree is not containing left or right child or it is a leaf node then that absence of child node can be represented by the null pointers. The space engaged by

Binary search technique, Q. Describe the basic concept of binary search tec...

Q. Describe the basic concept of binary search technique? Is it more efficient than the sequential search?         Ans : The bin ary search technique:- This tec

Efficient way of storing two symmetric matrices, Explain an efficient way o...

Explain an efficient way of storing two symmetric matrices of the same order in memory. A n-square matrix array is said to be symmetric if a[j][k]=a[k][j] for all j and k. For

Deletion algorithm for dequeue, Deletion Algorithm for dequeue Step 1:...

Deletion Algorithm for dequeue Step 1: [check for underflow]   If front = 0 and rear = 0   Output "underflow" and return Step 2: [delete element at front end]   If front

Algorithm, give me algorithm of simple interest

give me algorithm of simple interest

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