-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.c
43 lines (37 loc) · 858 Bytes
/
search.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
#include <stdio.h>
#include <stdlib.h>
int linearSearch(int* arr, int key, int size) {
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
int binarySearch(int* arr, int key, int size) {
int start = 0, end = size - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return -1;
}
int main() {
int *arr = (int*)malloc(20 * sizeof(int));
int key;
printf("Enter a key: ");
scanf("%d", &key);
for (int i = 0; i < 20; i++) {
arr[i] = (rand() % 50) + 1;
}
linearSearch(arr, key, 20);
binarySearch(arr, key,10);
free(arr);
return 0;
}