Skip to content

Commit 77fb870

Browse files
authored
Merge pull request #246 from TheAlgorithms/revert-245-master
Revert "Update iterBinarySearch"
2 parents 9087f67 + f5c0813 commit 77fb870

File tree

2 files changed

+6
-19
lines changed

2 files changed

+6
-19
lines changed

searches/binary_search.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ func iterBinarySearch(array []int, target int, lowIndex int, highIndex int) int
1818
startIndex := lowIndex
1919
endIndex := highIndex
2020
var mid int
21-
for startIndex <= endIndex {
22-
mid = int(startIndex + (endIndex)/2)
21+
for startIndex < endIndex {
22+
mid = int(startIndex + (endIndex - startIndex))
2323
if array[mid] > target {
24-
endIndex = mid - 1
24+
endIndex = mid
2525
} else if array[mid] < target {
26-
startIndex = mid + 1
26+
startIndex = mid
2727
} else {
2828
return mid
2929
}

searches/search_test.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ type searchTest struct {
1212
var searchTests = []searchTest{
1313
//Sanity
1414
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 5, 4, "Sanity"},
15-
//First
16-
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 1, 0, "First"},
17-
//Last
18-
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 10, 9, "Last"},
1915
//Absent
2016
{[]int{1, 4, 5, 6, 7, 10}, 25, -1, "Absent"},
2117
//Empty slice
@@ -26,16 +22,7 @@ func TestBinarySearch(t *testing.T) {
2622
for _, test := range searchTests {
2723
actual := binarySearch(test.data, test.key, 0, len(test.data)-1)
2824
if actual != test.expected {
29-
t.Errorf("test %s failed: expected '%d', get '%d'", test.name, test.expected, actual)
30-
}
31-
}
32-
}
33-
34-
func TestIterBinarySearch(t *testing.T) {
35-
for _, test := range searchTests {
36-
actual := iterBinarySearch(test.data, test.key, 0, len(test.data)-1)
37-
if actual != test.expected {
38-
t.Errorf("test %s failed: expected '%d', get '%d'", test.name, test.expected, actual)
25+
t.Errorf("test %s failed", test.name)
3926
}
4027
}
4128
}
@@ -44,7 +31,7 @@ func TestLinearSearch(t *testing.T) {
4431
for _, test := range searchTests {
4532
actual := linearSearch(test.data, test.key)
4633
if actual != test.expected {
47-
t.Errorf("test %s failed: expected '%d', get '%d'", test.name, test.expected, actual)
34+
t.Errorf("test %s failed", test.name)
4835
}
4936
}
5037
}

0 commit comments

Comments
 (0)