-
Notifications
You must be signed in to change notification settings - Fork 1
/
013_Array_Binary_Search.c
66 lines (53 loc) · 1.98 KB
/
013_Array_Binary_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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Question : Search an element from array using Binary search method;
=========================================================================
Binary Search Algorithm: The basic steps to perform Binary Search are:
---> Sort the array in ascending order.
---> Set the low index to the first element of the array and the high index to the last element.
---> Set the middle index to the average of the low and high indices.
---> If the element at the middle index is the target element, return the middle index.
---> If the target element is less than the element at the middle index, set the high index to the middle index – 1.
---> If the target element is greater than the element at the middle index, set the low index to the middle index + 1.
---> Repeat steps 3-6 until the element is found or it is clear that the element is not present in the array;
Code Below ::::::::
--------------------------------------------------------------------------
#include<stdio.h>
int linearSearch(int arr[], int size, int element){
for (int i = 0; i < size; i++)
{
if(arr[i]==element){
return i;
}
}
return -1;
}
int binarySearch(int arr[], int size, int element){
int low, mid, high;
low = 0;
high = size-1;
// Keep searching until low <= high
while(low<=high){
mid = (low + high)/2;
if(arr[mid] == element){
return mid;
}
if(arr[mid]<element){
low = mid+1;
}
else{
high = mid -1;
}
}
return -1;
}
int main(){
// Unsorted array for linear search
// int arr[] = {1,3,5,56,4,3,23,5,4,54634,56,34};
// int size = sizeof(arr)/sizeof(int);
// Sorted array for binary search
int arr[] = {1,3,5,56,64,73,123,225,444};
int size = sizeof(arr)/sizeof(int);
int element = 444;
int searchIndex = binarySearch(arr, size, element);
printf("The element %d was found at index %d \n", element, searchIndex);
return 0;
}