-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearch.c
62 lines (46 loc) · 1.2 KB
/
BinarySearch.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
59
60
61
62
//C Program to do Binary Search using function
#include<stdio.h>
//global variables
//function to search
int BinarySearch(int arr[], int lower, int higher, int skey){
while(lower <= higher){
int mid = (lower + higher) / 2;
if(arr[mid] == skey){
return mid;
break;
}
if(arr[mid] < skey){
lower = mid + 1;
}
else{
higher = mid - 1;
}
mid = (lower + higher) / 2;
}
if(lower > higher) return -1;
}
//main function
int main(){
int array[10], limit, key, i, low, high, mid;
char choice;
do{
//getting array size
printf("Enter array limit(max 10): ");
scanf("%d", &limit);
//getting array elements
printf("Enter sorted array elements:\n");
for(i=0; i<limit; i++){
printf("Element %d: ", i+1);
scanf("%d", &array[i]);
}
//getting search key
printf("Enter search key: ");
scanf("%d", &key);
if(BinarySearch(array[10], low, high, key)) printf("Element %d is found at position %d\n", mid);
else
printf("Element is not found on the list\n");
printf("\nEnter Your Choice \nFor SEARCHING Type s \nFor EXIT, press any letter\nChoice: ");
scanf(" %c", &choice);
}while(choice == 'S' || choice == 's');
}
//pushed only the sample, the program is not complete or correct