-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearch.kt
91 lines (84 loc) · 2.4 KB
/
BinarySearch.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package search
/**
* name: binary search algorithm
*
* description: one of the most important features of this algorithm
* is that it only works for sorted lists and arrays
*
* time: log(n)
* amount of memory: 1
*/
class BinarySearch<T : Comparable<T>> : Search<T> {
/**
* finds the left border to insert an element into a sorted array
*
* @array - sorted array to be searched
* @element - the element for which the left border is being searched
*/
fun leftBound(array: Array<T>, element: T) : Int {
var left = -1
var right = array.size
while ((right - left) > 1) {
val middle = left + (right - left) / 2
if (element > array[middle]) {
left = middle
} else {
right = middle
}
}
return left
}
/**
* finds the right border to insert an element into a sorted array
*
* @array - sorted array to be searched
* @element - the element for which the right border is being searched
*/
fun rightBound(array: Array<T>, element: T) : Int {
var left = -1
var right = array.size
while ((right - left) > 1) {
val middle = (right + left) / 2
if (element > array[middle]) {
left = middle
} else {
right = middle
}
}
return right
}
/**
* checks if an element is in an array
*
* @array - the array in which we are looking for the element
* @element - search element
*
* @return returns true if the element is present in the array, false otherwise
*/
override fun exists(array: Array<T>, element: T): Boolean {
return search(array, element) != -1
}
/**
* searches for an element in an array
*
* @array - array
* @element - search element
*
* @return returns the index of the searched element or -1
*/
override fun search(array: Array<T>, element: T) : Int {
var left = 0
var right = array.size - 1
while (left <= right) {
val middle = (left + right) / 2
if (array[middle] < element) {
left = middle + 1
} else if (array[middle] > element) {
right = middle
} else {
return middle
}
}
return -1
}
}