Testing.

Saturday, 24 March 2018

C programming language: C program to find an element in the array using binary search.

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

int main()
{
int *arr, SIZE, max,min, mid, flag=0;
int data, pos, i;
printf("\n Enter Array Size: ");
scanf("%d",&SIZE);
arr=(int *)calloc(SIZE,sizeof(int));
printf("\nEnter Array Elements: ");
for(i=0;i<SIZE;i++)
scanf("%d",&arr[i]);
printf ("\nEnter Data to be search: ");
scanf("%d",&data);
max=SIZE-1;
for(min=0;min<=max;min++)
{
if(arr[min]==data)
{
pos=min+1;
flag=1;
break;
}
mid=(min+max)/2;
if(arr[mid]<=data)
{
if(arr[mid]==data)
{
pos=mid+1;
flag=1;
break;
}
min=mid;
}
else
{
max=mid-1;
min=0;
}
}
if(flag==1)
printf("\n %d is found at %d Position: ",data,pos);
else
printf("\n %d IS NOT FOUND: ",data);
free(arr);
arr=NULL;
getch();
return 0;
}

2 comments:

  1. Play Roulette for free at Choegocasino Casino
    You are guaranteed to have your choegocasino favorite roulette games and if you deccasino win, you are not welcome to play septcasino Roulette for real money. Play this game for free.

    ReplyDelete