Skip to content

Commit 616448a

Browse files
committed
Add Binary Search in list
1 parent 286aeb8 commit 616448a

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@
6363
4. Quick Sort – [solution](src/main/kotlin/ru/romanow/algorithms/QuickSort.kt), [test](src/test/kotlin/ru/romanow/algorithms/QuickSortTest.kt)
6464
5. Merge Sort – [solution](src/main/kotlin/ru/romanow/algorithms/MergeSort.kt), [test](src/test/kotlin/ru/romanow/algorithms/MergeSortTest.kt)
6565
6. Binary Search Tree – [solution](src/main/kotlin/ru/romanow/algorithms/BinarySearchTree.kt), [test](src/test/kotlin/ru/romanow/algorithms/BinarySearchTreeTest.kt)
66-
7. Pre-order Deep First Search Traversal – [solution](src/main/kotlin/ru/romanow/algorithms/PreOrderDeepFirstSearch.kt), [test](src/test/kotlin/ru/romanow/algorithms/PreOrderDeepFirstSearchTest.kt)
67-
8. In-order Deep First Search Traversal – [solution](src/main/kotlin/ru/romanow/algorithms/InOrderDeepFirstSearch.kt), [test](src/test/kotlin/ru/romanow/algorithms/InOrderDeepFirstSearchTest.kt)
68-
9. Post-order Deep First Search Traversal – [solution](src/main/kotlin/ru/romanow/algorithms/PostOrderDeepFirstSearch.kt), [test](src/test/kotlin/ru/romanow/algorithms/PostOrderDeepFirstSearchTest.kt)
69-
10. Breadth First Search Traversal – [solution](src/main/kotlin/ru/romanow/algorithms/BreadthFirstSearch.kt), [test](src/test/kotlin/ru/romanow/algorithms/BreadthFirstSearchTest.kt)
66+
7. Binary Search List – [solution](src/main/kotlin/ru/romanow/algorithms/BinarySearchList.kt), [test](src/test/kotlin/ru/romanow/algorithms/BinarySearchListTest.kt)
67+
8. Pre-order Deep First Search Traversal – [solution](src/main/kotlin/ru/romanow/algorithms/PreOrderDeepFirstSearch.kt), [test](src/test/kotlin/ru/romanow/algorithms/PreOrderDeepFirstSearchTest.kt)
68+
9. In-order Deep First Search Traversal – [solution](src/main/kotlin/ru/romanow/algorithms/InOrderDeepFirstSearch.kt), [test](src/test/kotlin/ru/romanow/algorithms/InOrderDeepFirstSearchTest.kt)
69+
10. Post-order Deep First Search Traversal – [solution](src/main/kotlin/ru/romanow/algorithms/PostOrderDeepFirstSearch.kt), [test](src/test/kotlin/ru/romanow/algorithms/PostOrderDeepFirstSearchTest.kt)
70+
11. Breadth First Search Traversal – [solution](src/main/kotlin/ru/romanow/algorithms/BreadthFirstSearch.kt), [test](src/test/kotlin/ru/romanow/algorithms/BreadthFirstSearchTest.kt)
7071

7172
[//]: # (@formatter:on)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ru.romanow.algorithms
2+
3+
class BinarySearchList {
4+
fun find(list: List<Int>, target: Int, left: Int = 0, right: Int = list.size - 1): Int {
5+
if (left > right) {
6+
return -1
7+
}
8+
val middle = left + (right - left) / 2
9+
return when {
10+
list[middle] == target -> middle
11+
list[middle] < target -> find(list, target, middle + 1, right)
12+
else -> find(list, target, left, middle - 1)
13+
}
14+
}
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ru.romanow.algorithms
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.junit.jupiter.api.extension.ExtensionContext
5+
import org.junit.jupiter.params.ParameterizedTest
6+
import org.junit.jupiter.params.provider.Arguments
7+
import org.junit.jupiter.params.provider.ArgumentsProvider
8+
import org.junit.jupiter.params.provider.ArgumentsSource
9+
import java.util.stream.Stream
10+
11+
class BinarySearchListTest {
12+
13+
@ArgumentsSource(ValueProvider::class)
14+
@ParameterizedTest(name = "#{index} – Search {1} in tree {0}")
15+
fun flatten(list: List<Int>, target: Int, result: Int?) {
16+
val obj = BinarySearchList()
17+
assertThat(obj.find(list, target)).isEqualTo(result)
18+
}
19+
20+
internal class ValueProvider : ArgumentsProvider {
21+
override fun provideArguments(context: ExtensionContext): Stream<Arguments> =
22+
Stream.of(
23+
Arguments.of(listOf(1, 3, 5, 7, 10, 12, 15), 3, 1),
24+
Arguments.of(listOf(1, 3, 5, 7, 10, 12, 15), 14, -1)
25+
)
26+
}
27+
}

src/test/kotlin/ru/romanow/algorithms/BinarySearchTreeTest.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ class BinarySearchTreeTest {
1313

1414
@ArgumentsSource(ValueProvider::class)
1515
@ParameterizedTest(name = "#{index} – Search {1} in tree {0}")
16-
fun flatten(items: List<Int?>, target: Int, result: Int?) {
16+
fun flatten(items: List<Int>, target: Int, result: Int?) {
1717
val root = buildTreeFromList(items)
18-
// printTree(root)
19-
2018
val obj = BinarySearchTree()
2119
assertThat(obj.find(root, target)?.value).isEqualTo(result)
2220
}

0 commit comments

Comments
 (0)