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

Illustrate the operations of the symbol abstract data type, The operations ...

The operations of the Symbol ADT The operations of the Symbol ADT are the following. a==b-returns true if and only if symbols a and bare identical. a symbol bin Unico

Computer arhitecture, The controversy of RISC versus CISC never ends. Suppo...

The controversy of RISC versus CISC never ends. Suppose that you represent an advocate for the RISC approach; write at least a one-page critic of the CISC approach showing its disa

Applications of binary trees, In computer programming, Trees are utilized ...

In computer programming, Trees are utilized enormously. These can be utilized for developing database search times (binary search trees, AVL trees, 2-3 trees, red-black trees), Gam

Define merge sort, Define Merge Sort  Merge sort is a perfect example ...

Define Merge Sort  Merge sort is a perfect example of a successful application of the divide and conquer method. It sorts a given array A[0...n-l] by separating it into two ha

Algorithm for sorting a deck of cards, What is wrong with the following alg...

What is wrong with the following algorithm for sorting a deck of cards (considering the basic properties of algorithms)? I. Put the cards together into a pile II. For each ca

Binary search tree, Objectives The purpose of this project is to give yo...

Objectives The purpose of this project is to give you significant exposure to Binary Search Trees (BST), tree traversals, and recursive code. Background An arbitrary BST i

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

Bayesian statistics question, Suppose that there is a Beta(2,2) prior distr...

Suppose that there is a Beta(2,2) prior distribution on the probability theta that a coin will yield a "head" when spun in a specified manner. The coin is independently spun 10 tim

Interest, I =PR/12 Numbers of years .Interest rate up to 1yrs ...

I =PR/12 Numbers of years .Interest rate up to 1yrs . 5.50 up to 5yrs . 6.50 More than 5 yrs . 6.75 design an algorithm based on the above information

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