Skip to content

Commit

Permalink
search
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCai1111 committed Dec 29, 2016
1 parent bff2dd7 commit 8186abe
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 1 deletion.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ script:
- go test -v -race -coverprofile=avl.coverprofile ./avl-tree
- go test -v -race -coverprofile=pat.coverprofile ./pat-tree
- go test -v -race -coverprofile=pat.coverprofile ./sort
- go test -v -race -coverprofile=pat.coverprofile ./search
- gover
- goveralls -coverprofile=gover.coverprofile -service=travis-ci
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ cover:
go test -v -race -coverprofile=avl.coverprofile ./avl-tree
go test -v -race -coverprofile=pat.coverprofile ./pat-tree
go test -v -race -coverprofile=pat.coverprofile ./sort
go test -v -race -coverprofile=pat.coverprofile ./search
gover
go tool cover -html=gover.coverprofile
rm -rf *.coverprofile
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ A collection of useful and concise Golang data structures.
- [Quick sort](https://github.com/DavidCai1993/datastructures.go/tree/master/sort/quick_sort.go)
- [Selection sort](https://github.com/DavidCai1993/datastructures.go/tree/master/sort/selection_sort.go)
- [Shell sort](https://github.com/DavidCai1993/datastructures.go/tree/master/sort/shell_sort.go)
- [Search algorithms](https://github.com/DavidCai1993/datastructures.go/tree/master/search)
- [Binary search](https://github.com/DavidCai1993/datastructures.go/tree/master/search/binary_search.go)
- [Linear search](https://github.com/DavidCai1993/datastructures.go/tree/master/search/linear_search.go)
44 changes: 44 additions & 0 deletions search/binary_search.go
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
}
48 changes: 48 additions & 0 deletions search/binary_search_test.go
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)
})
}
16 changes: 16 additions & 0 deletions search/linear_search.go
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
}
42 changes: 42 additions & 0 deletions search/linear_search_test.go
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)
})
}
2 changes: 1 addition & 1 deletion sort/quick_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ds "github.com/DavidCai1993/datastructures.go"

// QuickSort sorts the given []Comparable by quick sort algorithm.
// Time complexity O(nlogn)
// Space complexity O(nlogn)
// Space complexity O(n)
func QuickSort(c ds.Comparables) ds.Comparables {
if len(c) <= 1 {
return c
Expand Down

0 comments on commit 8186abe

Please sign in to comment.