Skip to content

Commit e417bbe

Browse files
Merge branch 'main' into feature/dijkstra_algorithm
2 parents 64700cb + 21a2d2a commit e417bbe

11 files changed

Lines changed: 1784 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Algorithm: [Binary Search]
3+
* Description: [Binary search is an efficient algorithm used to find the position of a target value within a sorted array or list. It works on the principle of repeatedly dividing the search interval in half.]
4+
* Time Complexity :
5+
* Best Case : O(1) // when given array is already sorted.
6+
* Average Case : O(log n) //when given array is in random order
7+
* Worst Case : O(log n) // when given array is in reverse order
8+
* Space Complexity:
9+
* Worst : 0(1)
10+
* Author: [Kaustubh Udavant]
11+
*/
12+
13+
#include <stdio.h>
14+
#define SIZE 10
15+
int BinarySearch(int [],int);
16+
int main()
17+
{
18+
int a[SIZE]={3,5,9,11,15,17,22,25,37,68},key,pos;
19+
printf("Enter the Search Key\n");
20+
scanf("%d",&key);
21+
pos=BinarySearch(a,key);
22+
if(pos==-1)
23+
printf("The search key is not in the array\n");
24+
else
25+
printf("The search key %d is at location %d\n",key,pos);
26+
return 0;
27+
}
28+
29+
int BinarySearch (int A[],int skey)
30+
{
31+
int low=0,high=SIZE-1,middle;
32+
while (low <=high){
33+
middle=(low+high)/2;
34+
if(skey==A[middle])
35+
return middle;
36+
else if(skey <A[middle])
37+
high=middle-1;
38+
else
39+
low=middle+1;
40+
}
41+
return -1;
42+
}

0 commit comments

Comments
 (0)