answersLogoWhite

0

Binary search using pointer in c?

Updated: 8/11/2023
User Avatar

Wiki User

12y ago

Best Answer

#include
#include
#include
void main()
{
int a[10]={1,2,3,10,11,13,16,21,46,73};
int mid,lower=0,upper=9,num,flag=1;
clrscr();
printf("enter the number to search");
scanf("%d",&num);
printf("\n the list of the data is");
printf("%-20s","\ndata);
for(num=0;num<=upper;num++)
printf("%3d",a[num]);
printf("%-20s","\nindexno");
for(num=0;num<=upper;num++)
printf("%3d",num);
for(mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2)
{
if(a[mid]==num)
{
printf("the number is at position %d in array",mid);
flag=0;
break;
}
if(a[mid]>num)
upper=mid-1;
else
lower=mid+1;
}
if(flag)
printf("element is not present in the array");
getch();
}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

#include<conio.h>

#include<stdio.h>

void main()

{

int n,a[100],i,j,k,t,*p,*beg,*last,*mid,elmnt;

char ch;

clrscr();

printf("Enter no of elements : ");

scanf("%d",&n);

printf("\nEnter the elmnts :\n");

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

scanf("%d",&a[i]);

p=a;

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

{

for(j=i+1;j<n;j++)

{

if(*(p+i)>*(p+j))

{

t=*(p+i);

*(p+i)=*(p+j);

*(p+j)=t;

}

}

}

do{clrscr();

printf("\nSorted Array is:\n");

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

printf("%d\n",a[i]);

beg=&a[0];

last=&a[n-1];

printf("\nEnter element to be searched for : ");

scanf("%d",&elmnt);

while(*mid!=elmnt&&beg<=last)

{mid=beg+(last-beg)/2;

if(*mid<elmnt)

beg=mid+1;

if(*mid>elmnt)

last=mid-1;

}

if(*mid==elmnt)

printf("\nElement %d found ",elmnt);

else printf("\nElement not found ");

printf("\n\nPress <y> to search for another element..");

ch=getch();

}while(ch=='y');

}

----------------------------------------------------------------------------

Mithu

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Binary search using pointer in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Give you the algorithm of creating a new binary search tree using c?

i want to know how to give the algorithm password in a computer ?


What is binary search in data structure using c?

a tree which has atmost two nodes is called binary tree binary search tree is a binary tree which satisfies the following 1.every node in tree must be distinct 2.values in right subtree &gt; value at root 3.values in left subtree &lt; value at root 4.left,right subtrees must be binary search trees


Best first search program in c?

The best search programs to attempt writing in C are the following: Linear search (simplest), Binary search (faster) Hash search (fastest).


Code for binary trees written in C using graphics?

cg code for binary tree


How do you write a c program to convert binary to decimal by using while statement?

write a c++ program to convert binary number to decimal number by using while statement


What is NULL array in C Language?

There is no "NULL array" as such, you may take a pointer to an array and set it to NULL (binary 0) e.g. int* foo; // Declare a pointer foo = malloc( 40 * sizeof(int)); //Allocate an array of 40 integers pointed to by "foo" foo = NULL; //Set the pointer to NULL, if you're using a garbage collector this should trigger an automatic free() of the memory allocated to the array. If you are NOT using a garbage collector (which is more common in C) this line is a memory leak.


How do you change scrin coulor using far pointer in c ganguage?

brown


What is a tree recursion in C programming?

A binary tree is made of nodes, where each node contains a "left" pointer, a "right" pointer, and a data element. The "root" pointer points to the topmost node in the tree. The left and right pointers recursively point to smaller "subtrees" on either side. The formal recursive definition is: a binary tree is either empty (represented by a null pointer), or is made of a single node, where the left and right pointers (recursive definition ahead) each point to a binary tree. Tree recursion describes a class of algorithms for accessing binary trees, exploiting their inherently recursive nature. answer by narayan nyaupane kathmandu, Nepal


What are the Advantages of binary search on linear search in c?

(i) Binary search can interact poorly with the memory hierarchy (i.e. caching), because of its random-access nature. For in-memory searching, if the interval to be searching is small, a linear search may have superior performance simply because it exhibits better locality of reference. (ii) Binary search algorithm employs recursive approach and this approach requires more stack space. (iii) Programming binary search algorithm is very difficult and error prone (Kruse, 1999).


Call by reference using pointer in c plus plus?

Example: void foo( MyClass&amp; object ){} // function with call by reference signature MyClass* p = new MyClass(); // instantiate a pointer to MyClass foo( *p ); // call by reference using the pointer


How is the code return in higher programming languages like C and C converted into binary language?

By using a compiler.


Write a C program that takes a binary file as input and finds error check using different mechanisms?

write a c program that takes a binary file as input and finds error check using different mechanisms.