Skip to content

Commit 681a112

Browse files
author
phyex0
committed
BinarySearch added.
1 parent 35159b4 commit 681a112

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.company.Algorithms;
2+
3+
/*
4+
* Binary Search:
5+
* Best case = o(1)
6+
* Average case = o(log(n))
7+
* Worst case = o(log(n))
8+
*
9+
*
10+
* Making a search on sorted array.
11+
* */
12+
13+
14+
public class BinarySearch {
15+
16+
public static int binarySearch(int[] arr,int val){
17+
int index=-1, left = 0, right=arr.length, mid;
18+
19+
while(right>1){
20+
mid = 1+(left-right)/2;
21+
22+
if(arr[mid]== val)
23+
index= mid;
24+
25+
else if(arr[mid] > val)
26+
left--;
27+
28+
else
29+
right--;
30+
31+
32+
}
33+
34+
return index;
35+
36+
37+
}
38+
}

src/com/company/Main.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ public class Main {
88

99
public static void main(String[] args) {
1010
// write your code here
11-
int[] arr = new int[]{3,2,1};
11+
int[] arr = new int[]{10,9,9,8,7,5,4,3,2,1};
12+
BubbleSort.bubbleSort(arr);
1213

13-
System.out.println(LinearSearch.linearSearch(arr,5));
14+
System.out.println(LinearSearch.linearSearch(arr,10));
1415

1516

1617

0 commit comments

Comments
 (0)