Skip to content

Commit dd20d85

Browse files
committed
Reworked searches so they are in a Collections extensions file; linear search now works on any Iterable
1 parent 87deb3b commit dd20d85

File tree

2 files changed

+77
-63
lines changed

2 files changed

+77
-63
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
package com.lillicoder.algorithms.search
2-
3-
/**
4-
* Performs a binary search on this list to find the position of the given element.
5-
* @param toFind Element to find.
6-
* @return List position or -1 if there is no such element.
7-
*/
8-
fun <T : Comparable<T>> List<T>.binarySearch(toFind: T): Int {
9-
var low = 0
10-
var high = size - 1
11-
while (low <= high) {
12-
val middle = (low + high) ushr 1
13-
val midElement = get(middle)
14-
15-
if (midElement == toFind) {
16-
return middle
17-
} else if (midElement < toFind) {
18-
low = middle + 1
19-
} else {
20-
high = middle - 1
21-
}
22-
}
23-
24-
return -1
25-
}
26-
27-
/**
28-
* Performs a linear search on this list to find the position of the given element.
29-
* @param toFind Element to find.
30-
* @return List position or -1 if there is no such element.
31-
*/
32-
fun <T : Comparable<T>> List<T>.linearSearch(toFind: T): Int {
33-
forEachIndexed { index, element ->
34-
if (element == toFind) return index
35-
}
36-
37-
return -1
38-
}
1+
package com.lillicoder.algorithms.search
2+
3+
/**
4+
* Performs a [linear search](https://en.wikipedia.org/wiki/Linear_search) to find the position of the given element.
5+
* @param toFind Element to find.
6+
* @return Position or -1 if there is no such element.
7+
*/
8+
fun <T : Comparable<T>> Iterable<T>.linearSearch(toFind: T): Int {
9+
forEachIndexed { index, element ->
10+
if (element == toFind) return index
11+
}
12+
13+
return -1
14+
}
15+
16+
/**
17+
* Performs a [binary search](https://en.wikipedia.org/wiki/Binary_search) find the position of the given element.
18+
* @param toFind Element to find.
19+
* @return Position or -1 if there is no such element.
20+
*/
21+
fun <T : Comparable<T>> List<T>.binarySearch(toFind: T): Int {
22+
var low = 0
23+
var high = size - 1
24+
while (low <= high) {
25+
val middle = (low + high) ushr 1
26+
val midElement = get(middle)
27+
28+
if (midElement == toFind) {
29+
return middle
30+
} else if (midElement < toFind) {
31+
low = middle + 1
32+
} else {
33+
high = middle - 1
34+
}
35+
}
36+
37+
return -1
38+
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
1-
package com.lillicoder.algorithms.search
2-
3-
import kotlin.test.Test
4-
import kotlin.test.assertEquals
5-
6-
/**
7-
* Unit tests for list search extensions.
8-
*/
9-
internal class SearchTest {
10-
private val list = listOf("delta", "tau", "zeta", "alpha", "gamma", "alpha", "beta", "epsilon")
11-
12-
@Test
13-
fun binarySearch() {
14-
val expected = 6
15-
val actual = list.sorted().binarySearch("tau")
16-
assertEquals(expected, actual)
17-
}
18-
19-
@Test
20-
fun linearSearch() {
21-
val expected = 3
22-
val actual = list.linearSearch("alpha")
23-
assertEquals(expected, actual)
24-
}
25-
}
1+
package com.lillicoder.algorithms.search
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
6+
/**
7+
* Unit tests for list search extensions.
8+
*/
9+
internal class CollectionsKtTest {
10+
private val list = listOf("delta", "tau", "zeta", "alpha", "gamma", "alpha", "beta", "epsilon")
11+
12+
@Test
13+
fun `Binary search finds expected element`() {
14+
val expected = 6
15+
val actual = list.sorted().binarySearch("tau")
16+
assertEquals(expected, actual)
17+
}
18+
19+
@Test
20+
fun `Binary search for invalid element returns -1`() {
21+
val expected = -1
22+
val actual = list.binarySearch("test")
23+
assertEquals(expected, actual)
24+
}
25+
26+
@Test
27+
fun `Linear search finds expected element`() {
28+
val expected = 3
29+
val actual = list.linearSearch("alpha")
30+
assertEquals(expected, actual)
31+
}
32+
33+
@Test
34+
fun `Linear search for invalid element returns -1`() {
35+
val expected = -1
36+
val actual = list.linearSearch("test")
37+
assertEquals(expected, actual)
38+
}
39+
}

0 commit comments

Comments
 (0)