Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 04d623a

Browse files
binary search
1 parent b3d5152 commit 04d623a

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

‎Searching/binarySearch.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
class binarySearch {
3+
4+
public static void main(String[] args) {
5+
int arr[] = {1, 2, 3, 4, 5, 6, 57, };
6+
int target = 6;
7+
int result = binarySearching(arr, target);
8+
System.out.println("the target element index is: " + result);
9+
}
10+
11+
public static int binarySearching(int arr[], int target) {
12+
13+
int start = 0;
14+
int end = arr.length - 1;
15+
int mid;
16+
17+
while (start <= end) {
18+
mid = (start + end) / 2;
19+
if (arr[mid] == target) {
20+
return mid;
21+
} else {
22+
if(target < arr[mid]){
23+
end = mid - 1;
24+
}else{
25+
start = mid + 1;
26+
}
27+
}
28+
}
29+
return -1;
30+
}
31+
}
32+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /