diff options
author | Ruben Pollan <meskio@sindominio.net> | 2021-01-25 17:56:26 +0100 |
---|---|---|
committer | kali kaneko (leap communications) <kali@leap.se> | 2021-02-25 21:34:10 +0100 |
commit | 0b50eca211bda4d0c1cb28973126f0269288bb71 (patch) | |
tree | 09d374ade952a2366292ec6d8ab8b174b0581a2c /vendor/github.com/hongshibao/go-algo/selection.go | |
parent | ec03343a76291f10fd2216ef74addff98b9701f9 (diff) |
Use go modules & and devendorize
spring clean for menshen!
Diffstat (limited to 'vendor/github.com/hongshibao/go-algo/selection.go')
-rw-r--r-- | vendor/github.com/hongshibao/go-algo/selection.go | 57 |
1 files changed, 0 insertions, 57 deletions
diff --git a/vendor/github.com/hongshibao/go-algo/selection.go b/vendor/github.com/hongshibao/go-algo/selection.go deleted file mode 100644 index b17fe70..0000000 --- a/vendor/github.com/hongshibao/go-algo/selection.go +++ /dev/null @@ -1,57 +0,0 @@ -package algo - -import ( - "math/rand" - "sort" - - "github.com/dropbox/godropbox/errors" -) - -// [left, right] -func Partition(array sort.Interface, left int, right int, - pivotIndex int) (int, error) { - if pivotIndex < left || pivotIndex > right { - return -1, errors.New("Pivot index out of range") - } - if left < 0 || right >= array.Len() { - return -1, errors.New("Array index out of range") - } - lastIndex := right - array.Swap(pivotIndex, lastIndex) - leftIndex := left - for i := left; i < lastIndex; i++ { - if array.Less(i, lastIndex) { - array.Swap(leftIndex, i) - leftIndex++ - } - } - array.Swap(leftIndex, lastIndex) - return leftIndex, nil -} - -// k is started from 1 -func QuickSelect(array sort.Interface, k int) error { - if k <= 0 || k > array.Len() { - return errors.New("Parameter k is invalid") - } - k-- - left := 0 - right := array.Len() - 1 - for { - if left == right { - return nil - } - pivotIndex := left + rand.Intn(right-left+1) - pivotIndex, err := Partition(array, left, right, pivotIndex) - if err != nil { - return errors.Wrap(err, "Partition returns error") - } - if k == pivotIndex { - return nil - } else if k < pivotIndex { - right = pivotIndex - 1 - } else { - left = pivotIndex + 1 - } - } -} |