File tree Expand file tree Collapse file tree 2 files changed +18
-10
lines changed Expand file tree Collapse file tree 2 files changed +18
-10
lines changed Original file line number Diff line number Diff line change 1
1
int binary_search (int arr[],int size, int key)
2
2
{
3
- int low = 0 ;
3
+ int low, mid = 0 ;
4
4
int high = size - 1 ;
5
-
6
5
while (low <= high)
7
6
{
8
- mid = low + (high - low) / 2 ;
9
- // start from here
7
+ mid = low + (high - low) / 2 ;
8
+ if (arr[mid] == key)
9
+ return mid;
10
+
10
11
if (key < arr[mid])
11
12
{
12
- high = mid;
13
- mid = low + high / 2 ;
13
+ high = mid - 1 ;
14
14
}
15
- else if (key > arr[mid])
15
+ else
16
16
{
17
- low = mid;
18
- mid = low + high / 2 ;
17
+ low = mid + 1 ;
19
18
}
20
19
}
21
- return (arr[mid] == key) ? mid : -1 ;
20
+ return -1 ;
22
21
}
Original file line number Diff line number Diff line change
1
+ int linear_search (int arr[],int size, int key)
2
+ {
3
+ for (int i = 0 ; i < size; i++)
4
+ {
5
+ if (arr[i] == key)
6
+ return i;
7
+ }
8
+ return -1 ;
9
+ }
You can’t perform that action at this time.
0 commit comments