-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion29.c
58 lines (53 loc) · 1.09 KB
/
question29.c
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Program to implement binary search in an array
*/
#include <stdio.h>
#include <stdlib.h>
int findElmIterative(int *arr, int start, int end, int elm){
while(start < end){
int mid = (start+end)/2;
if(arr[mid] > elm){
end = mid - 1;
}else if(arr[mid] < elm){
start = mid + 1;
}else{
return mid;
}
}
return -1;
}
int findElmRecursive(int *arr, int start, int end, int elm){
if(start >= end){
return -1;
}
int mid = (start + end)/2;
if(arr[mid] > elm){
return findElmRecursive(arr,start,mid-1,elm);
}
if(arr[mid] < elm){
return findElmRecursive(arr,mid+1,end,elm);
}
return mid;
}
int main(){
int arr[] = {2,5,8,12,16,23,38,56,72,91};
int size = sizeof(arr)/sizeof(arr[0]);
int elm = 23;
int step;
printf("1. Do Iteration\n");
printf("2. Do Recursion\n");
scanf("%d",&step);
int index;
switch(step){
case 1: index = findElmIterative(arr,0, size-1, elm);
break;
case 2: index = findElmRecursive(arr,0,size-1,elm);
break;
}
if(index < 0){
printf("no such element is present\n");
}else{
printf("element is present at index %d\n", index);
}
return 0;
}