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 2aea536

Browse files
binary search implements
1 parent e13a6ea commit 2aea536

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

‎Searching/binarySearch.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
class binarySearch {
33

44
public static void main(String[] args) {
5-
int[] arr = {3, 5, 7, 9, 11, 13, 15, 17};
6-
int target_item = 13;
5+
int[] arr = {3, 5, 7, 9, 11, 13, 15, 17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49};
6+
int target_item = 43;
77
int result = binarySearching(arr, target_item);
88

99
if (result != -1) {
@@ -14,18 +14,26 @@ public static void main(String[] args) {
1414
}
1515

1616
public static int binarySearching(int[] a, int target) {
17+
int step = 0;
1718
int left = 0;
1819
int right = a.length - 1;
1920
while (left <= right) {
21+
step++;
2022
int mid = (left + right) / 2;
2123
if (a[mid] == target) {
24+
System.out.println("Total steps: " + step);
2225
return mid;
26+
2327
} else if (a[mid] < target) {
2428
left = mid + 1;
25-
}else{
29+
30+
} else {
2631
right = mid - 1;
2732
}
33+
2834
}
35+
2936
return -1;
37+
3038
}
3139
}

0 commit comments

Comments
(0)

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