Skip to content

Commit f03cf03

Browse files
author
github-actions
committed
Format: Google java formatter
1 parent abb10b1 commit f03cf03

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

Searching/BinarySearch/BinarySearch.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public static int search(int[] arr, int value) {
77

88
/**
99
* * Iterative Approach
10-
*
10+
*
1111
* * TC: O(log n)
1212
* * SC: O(1)
1313
*/
@@ -16,22 +16,17 @@ private static int binarySearch(int[] arr, int value, int low, int high) {
1616
while (low < high) {
1717
int mid = low + high - low / 2;
1818

19-
if (arr[mid] == value)
20-
return mid;
21-
22-
else if (arr[mid] < value)
23-
low = mid + 1;
24-
25-
else
26-
high = mid - 1;
19+
if (arr[mid] == value) return mid;
20+
else if (arr[mid] < value) low = mid + 1;
21+
else high = mid - 1;
2722
}
2823

2924
return -1;
3025
}
3126

3227
/**
3328
* * Recursive Approach
34-
*
29+
*
3530
* * TC: O(log n)
3631
* * SC: O(log n)
3732
*/
@@ -46,4 +41,4 @@ else if (arr[mid] < value)
4641

4742
// return binarySearch(arr, value, low, mid - 1);
4843
// }
49-
}
44+
}

Searching/BinarySearch/BinarySearchTest.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@
22

33
public class BinarySearchTest {
44
public static void main(String[] args) {
5-
5+
66
int position = BinarySearch.search(new int[] {2, 6, 8, 10, 12, 13}, 10);
77

88
// should be 3
99
System.out.println(
10-
position < 0 ?
11-
"\nValue not found in the array" :
12-
"\nValue found at position: " + position
13-
);
14-
10+
position < 0 ? "\nValue not found in the array" : "\nValue found at position: " + position);
11+
1512
position = BinarySearch.search(new int[] {2, 6, 8, 10, 12, 13}, 20);
1613

1714
// Not found
1815
System.out.println(
19-
position < 0 ?
20-
"\nValue not found in the array" :
21-
"\nValue found at position: " + position
22-
);
16+
position < 0 ? "\nValue not found in the array" : "\nValue found at position: " + position);
2317
}
24-
}
18+
}

0 commit comments

Comments
 (0)