-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.c
More file actions
35 lines (33 loc) · 853 Bytes
/
Copy pathBinarySearch.c
File metadata and controls
35 lines (33 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//
// main.c
// binary search
//
// Created by Vicky_Jha on 24/11/19.
// Copyright © 2019 test. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
int search ,first , last,mid;
int arr[] = {10,20,30,40,50,60,70,80,90,100};
first = 0;
last = sizeof(arr)/sizeof(arr[0]);
// printf("%d",last);
printf("Enter the element to be searched\n");
scanf("%d",&search);
while(first <= last)
{
mid = (first + last)/2;
if(search < arr[mid])
last = mid - 1;
else if(search > arr[mid])
first = mid + 1;
else if(search == arr[mid])
{
printf("Element %d found at location %d\n",search,mid+1);
exit(1);
}
}
printf("Element %d not found\n",search);
return 0;
}