File tree Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change
1
+ static int binarySearch(int[] arr, int target) {
2
+ int low = 0 ;
3
+ int high = arr.length - 1 ;
4
+
5
+ while(low <= high) {
6
+ int mid =(low + high) / 2;
7
+
8
+ if(arr[mid] == target) {
9
+ return mid;
10
+ }
11
+ else if(arr[mid] > target) {
12
+ high = mid - 1;
13
+ }
14
+ else {
15
+ low = mid + 1;
16
+ }
17
+
18
+ }
19
+ return -1;
20
+ }
21
+
Original file line number Diff line number Diff line change 8
8
9
9
public class SelectionSort implements SortAlgorithm {
10
10
11
+ /**
12
+ * This method swaps the two elements in the array
13
+ * @param arr, i, j The array for the swap and
14
+ the indexes of the to-swap elements
15
+ */
16
+ public void swap (T [] arr , int i , int j ) {
17
+ T temp = arr [i ];
18
+ arr [i ] = arr [j ];
19
+ arr [j ] = temp ;
20
+ }
21
+
11
22
/**
12
23
* This method implements the Generic Selection Sort
13
24
*
@@ -22,14 +33,14 @@ public <T extends Comparable<T>> T[] sort(T[] arr) {
22
33
int min = i ;
23
34
24
35
for (int j = i + 1 ; j < n ; j ++) {
25
- if (SortUtils . less (arr [j ], arr [ min ]) ) {
36
+ if (arr [ min ]. compareTo (arr [j ]) < 0 ) {
26
37
min = j ;
27
38
}
28
39
}
29
40
30
41
// Swapping if index of min is changed
31
42
if (min != i ) {
32
- SortUtils . swap (arr , i , min );
43
+ swap (arr , i , min );
33
44
}
34
45
}
35
46
You can’t perform that action at this time.
0 commit comments