Skip to content

Commit a47d37e

Browse files
authored
Merge pull request #186 from javaeeeee/binary-search
Binary search
2 parents edba888 + 1d8fa2c commit a47d37e

File tree

7 files changed

+204
-1
lines changed

7 files changed

+204
-1
lines changed

config.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
"robot-simulator",
5353
"bracket-push",
5454
"pythagorean-triplet",
55-
"binary-search-tree"
55+
"binary-search-tree",
56+
"binary-search"
5657
],
5758
"exercises": [
5859
{
@@ -299,6 +300,11 @@
299300
"slug": "binary-search-tree",
300301
"difficulty": 1,
301302
"topics": []
303+
},
304+
{
305+
"slug": "binary-search",
306+
"difficulty": 1,
307+
"topics": []
302308
}
303309
],
304310
"deprecated": [

exercises/binary-search/build.gradle

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
test {
13+
testLogging {
14+
exceptionFormat = 'full'
15+
events = ["passed", "failed", "skipped"]
16+
}
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
import java.util.List;
3+
4+
public class BinarySearch<T extends Comparable<T>> {
5+
6+
private List<T> array;
7+
private int arraySize;
8+
9+
public BinarySearch(List<T> array) {
10+
this.array = array;
11+
this.arraySize = array.size();
12+
}
13+
14+
public int indexOf(T value) {
15+
return search(value);
16+
}
17+
18+
public List<T> getArray() {
19+
return array;
20+
}
21+
22+
private int search(T value) {
23+
int left = 0;
24+
int right = this.arraySize - 1;
25+
int middle;
26+
T element;
27+
while (left <= right) {
28+
middle = (int) Math.floor(0.5 * (left + right));
29+
element = this.array.get(middle);
30+
if (value.compareTo(element) > 0) {
31+
left = middle + 1;
32+
} else if (value.compareTo(element) < 0) {
33+
right = middle - 1;
34+
} else {
35+
return middle;
36+
}
37+
}
38+
return -1;
39+
}
40+
}

exercises/binary-search/src/main/java/.keep

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
public class BinarySearch {
3+
4+
}

exercises/binary-search/src/test/java/.keep

Whitespace-only changes.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
2+
import java.util.ArrayList;
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import static org.junit.Assert.assertEquals;
7+
import org.junit.Ignore;
8+
import org.junit.Test;
9+
10+
public class BinarySearchTest {
11+
12+
public static final List<Integer> EMPTY_LIST
13+
= Collections.unmodifiableList(new ArrayList<Integer>(0));
14+
15+
public static final List<Integer> LIST_OF_UNIT_LENGTH
16+
= Collections.unmodifiableList(
17+
Arrays.asList(6)
18+
);
19+
20+
private static final List<Integer> SORTED_LIST
21+
= Collections.unmodifiableList(
22+
Arrays.asList(1, 3, 4, 6, 8, 9, 11)
23+
);
24+
25+
public static final List<Integer> SORTED_LIST_OF_ODD_LENGTH
26+
= Collections.unmodifiableList(
27+
Arrays.asList(1, 3, 5, 8, 13, 21, 34, 55,
28+
89, 144, 233, 377, 634)
29+
);
30+
31+
public static final List<Integer> SORTED_LIST_OF_EVEN_LENGTH
32+
= Collections.unmodifiableList(
33+
Arrays.asList(1, 3, 5, 8, 13, 21, 34, 55,
34+
89, 144, 233, 377)
35+
);
36+
37+
@Test
38+
public void findsAValueInAnArrayWithOneElement() {
39+
BinarySearch<Integer> sut = new BinarySearch<>(LIST_OF_UNIT_LENGTH);
40+
final int value = 6;
41+
final int actual = sut.indexOf(value);
42+
final int expected = 0;
43+
assertEquals(expected, actual);
44+
}
45+
46+
@Ignore
47+
@Test
48+
public void findsAValueInTheMiddleOfAnArray() {
49+
BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
50+
final int value = 6;
51+
final int actual = sut.indexOf(value);
52+
final int expected = 3;
53+
assertEquals(expected, actual);
54+
}
55+
56+
@Ignore
57+
@Test
58+
public void findsAValueAtTheBeginningOfAnArray() {
59+
BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
60+
final int value = 1;
61+
final int actual = sut.indexOf(value);
62+
final int expected = 0;
63+
assertEquals(expected, actual);
64+
}
65+
66+
@Ignore
67+
@Test
68+
public void findsAValueAtTheEndOfAnArray() {
69+
BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
70+
final int value = 11;
71+
final int actual = sut.indexOf(value);
72+
final int expected = 6;
73+
assertEquals(expected, actual);
74+
}
75+
76+
@Ignore
77+
@Test
78+
public void findsAValueInAnArrayOfOddLength() {
79+
BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST_OF_ODD_LENGTH);
80+
final int value = 144;
81+
final int actual = sut.indexOf(value);
82+
final int expected = 9;
83+
assertEquals(expected, actual);
84+
}
85+
86+
@Ignore
87+
@Test
88+
public void findsAValueInAnArrayOfEvenLength() {
89+
BinarySearch<Integer> sut
90+
= new BinarySearch<>(SORTED_LIST_OF_EVEN_LENGTH);
91+
final int value = 21;
92+
final int actual = sut.indexOf(value);
93+
final int expected = 5;
94+
assertEquals(expected, actual);
95+
}
96+
97+
@Ignore
98+
@Test
99+
public void identifiesThatAValueIsNotIncludedInTheArray() {
100+
BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
101+
final int value = 7;
102+
final int actual = sut.indexOf(value);
103+
final int expected = -1;
104+
assertEquals(expected, actual);
105+
}
106+
107+
@Ignore
108+
@Test
109+
public void aValueSmallerThanTheArraysSmallestValueIsNotIncluded() {
110+
BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
111+
final int value = 0;
112+
final int actual = sut.indexOf(value);
113+
final int expected = -1;
114+
assertEquals(expected, actual);
115+
}
116+
117+
@Ignore
118+
@Test
119+
public void aValueLargerThanTheArraysSmallestValueIsNotIncluded() {
120+
BinarySearch<Integer> sut = new BinarySearch<>(SORTED_LIST);
121+
final int value = 13;
122+
final int actual = sut.indexOf(value);
123+
final int expected = -1;
124+
assertEquals(expected, actual);
125+
}
126+
127+
@Ignore
128+
@Test
129+
public void nothingIsIncludedInAnEmptyArray() {
130+
BinarySearch<Integer> sut = new BinarySearch<>(EMPTY_LIST);
131+
final int value = 1;
132+
final int actual = sut.indexOf(value);
133+
final int expected = -1;
134+
assertEquals(expected, actual);
135+
}
136+
}

0 commit comments

Comments
 (0)