mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-25 06:49:36 +03:00
Other Binary way proposition
This commit is contained in:
parent
80496da206
commit
1440ee38fc
50
otherBinarySearch.c
Normal file
50
otherBinarySearch.c
Normal file
@ -0,0 +1,50 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define len 5
|
||||
|
||||
int binarySearch(int array[] , int len , int searchX)
|
||||
{
|
||||
|
||||
int pos = -1 , right , left , i = 0;
|
||||
|
||||
left = 0;
|
||||
right = len - 1;
|
||||
|
||||
for(i = 0; i < len ; i++)
|
||||
{
|
||||
pos = (left + right)/2;
|
||||
|
||||
if(array[pos] == searchX)
|
||||
return pos;
|
||||
else
|
||||
{
|
||||
if(array[pos] < searchX)
|
||||
right = pos - 1;
|
||||
else
|
||||
{
|
||||
left = pos + 1 ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void main(int argc , char *argv[])
|
||||
{
|
||||
int array[len] = { 5, 8 , 10 , 14 ,16};
|
||||
|
||||
int position;
|
||||
position = binarySearch(array , len , 5);
|
||||
|
||||
if( position < 0)
|
||||
printf("The number %d doesnt exist in array\n",5);
|
||||
else
|
||||
{
|
||||
printf("The number %d exist in array at position : %d \n",5,position);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user