-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bff2dd7
commit 8186abe
Showing
8 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package search | ||
|
||
import ( | ||
"math" | ||
|
||
ds "github.com/DavidCai1993/datastructures.go" | ||
) | ||
|
||
// BinarySearch finds the given element's location by binary search algorithm. | ||
// The list given must be sorted. | ||
// Time complexity O(logn) | ||
// Space complexity O(1) | ||
func BinarySearch(cs ds.Comparables, val ds.Comparable) int { | ||
if len(cs) == 0 { | ||
return -1 | ||
} | ||
|
||
lower, upper := 0, len(cs)-1 | ||
|
||
for upper-lower > 1 { | ||
mid := lower + int(math.Floor(float64(upper-lower)/2)) | ||
|
||
c := val.Compare(cs[mid]) | ||
|
||
if c == 0 { | ||
return mid | ||
} else if c < 0 { | ||
upper = mid - 1 | ||
} else { | ||
lower = mid + 1 | ||
} | ||
} | ||
|
||
// upper - lower == 1 | ||
if cs[lower].Compare(val) == 0 { | ||
return lower | ||
} | ||
|
||
if cs[upper].Compare(val) == 0 { | ||
return upper | ||
} | ||
|
||
return -1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package search | ||
|
||
import ( | ||
"testing" | ||
|
||
ds "github.com/DavidCai1993/datastructures.go" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBinarySearch(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
t.Run("found", func(t *testing.T) { | ||
i := BinarySearch(ds.Comparables{ | ||
ds.IntComparable(10), | ||
ds.IntComparable(14), | ||
ds.IntComparable(19), | ||
ds.IntComparable(27), | ||
ds.IntComparable(33), | ||
ds.IntComparable(35), | ||
ds.IntComparable(42), | ||
ds.IntComparable(44), | ||
ds.IntComparable(50), | ||
}, ds.IntComparable(42)) | ||
|
||
assert.Equal(6, i) | ||
}) | ||
|
||
t.Run("not found", func(t *testing.T) { | ||
i := BinarySearch(ds.Comparables{ | ||
ds.IntComparable(10), | ||
ds.IntComparable(14), | ||
ds.IntComparable(19), | ||
ds.IntComparable(27), | ||
ds.IntComparable(33), | ||
ds.IntComparable(35), | ||
ds.IntComparable(42), | ||
ds.IntComparable(44), | ||
ds.IntComparable(50), | ||
}, ds.IntComparable(99)) | ||
|
||
assert.Equal(-1, i) | ||
|
||
i = BinarySearch(ds.Comparables{}, ds.IntComparable(14)) | ||
|
||
assert.Equal(-1, i) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package search | ||
|
||
import ds "github.com/DavidCai1993/datastructures.go" | ||
|
||
// LinearSearch finds the given element's location by linear search algorithm. | ||
// Time complexity O(n) | ||
// Space complexity O(1) | ||
func LinearSearch(cs ds.Comparables, val ds.Comparable) int { | ||
for i, c := range cs { | ||
if c.Compare(val) == 0 { | ||
return i | ||
} | ||
} | ||
|
||
return -1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package search | ||
|
||
import ( | ||
"testing" | ||
|
||
ds "github.com/DavidCai1993/datastructures.go" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLinearSearch(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
t.Run("found", func(t *testing.T) { | ||
i := LinearSearch(ds.Comparables{ | ||
ds.IntComparable(14), | ||
ds.IntComparable(33), | ||
ds.IntComparable(27), | ||
ds.IntComparable(10), | ||
ds.IntComparable(35), | ||
ds.IntComparable(19), | ||
ds.IntComparable(42), | ||
ds.IntComparable(44), | ||
}, ds.IntComparable(42)) | ||
|
||
assert.Equal(6, i) | ||
}) | ||
|
||
t.Run("not found", func(t *testing.T) { | ||
i := LinearSearch(ds.Comparables{ | ||
ds.IntComparable(14), | ||
ds.IntComparable(33), | ||
ds.IntComparable(27), | ||
ds.IntComparable(10), | ||
ds.IntComparable(35), | ||
ds.IntComparable(19), | ||
ds.IntComparable(42), | ||
ds.IntComparable(44), | ||
}, ds.IntComparable(99)) | ||
|
||
assert.Equal(-1, i) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters