Organize algorithms into subpackages
This commit is contained in:
parent
4b090b48ea
commit
93d896e73b
|
@ -1,4 +1,4 @@
|
|||
package algorithms
|
||||
package search
|
||||
|
||||
// BinarySearch is an implementation of binary search algorithm.
|
||||
// Wikipedia: https://en.wikipedia.org/wiki/Binary_search_algorithm
|
|
@ -1,4 +1,4 @@
|
|||
package algorithms
|
||||
package search
|
||||
|
||||
import (
|
||||
"testing"
|
|
@ -1,27 +1,27 @@
|
|||
package algorithms
|
||||
package sort
|
||||
|
||||
// BinaryInsertionSort is an implementation of binary insertion sort algorithm.
|
||||
// Wikipedia: https://en.wikipedia.org/wiki/Insertion_sort#Variants
|
||||
func BinaryInsertionSort(a []int) []int {
|
||||
for i := 1; i < len(a); i++ {
|
||||
v := a[i]
|
||||
low := 0
|
||||
high := i
|
||||
first := 0
|
||||
last := i
|
||||
|
||||
for low < high {
|
||||
mid := low + (high-low)/2
|
||||
for first < last {
|
||||
mid := first + (last-first)/2
|
||||
if v < a[mid] {
|
||||
high = mid
|
||||
last = mid
|
||||
} else {
|
||||
low = mid + 1
|
||||
first = mid + 1
|
||||
}
|
||||
}
|
||||
|
||||
for j := i; j > low; j-- {
|
||||
for j := i; j > first; j-- {
|
||||
a[j] = a[j-1]
|
||||
}
|
||||
|
||||
a[low] = v
|
||||
a[first] = v
|
||||
}
|
||||
|
||||
return a
|
|
@ -1,4 +1,4 @@
|
|||
package algorithms
|
||||
package sort
|
||||
|
||||
import (
|
||||
"testing"
|
|
@ -1,4 +1,4 @@
|
|||
package algorithms
|
||||
package sort
|
||||
|
||||
// InsertionSort is an implementation of insertion sort algorithm.
|
||||
// Wikipedia: https://en.wikipedia.org/wiki/Insertion_sort
|
|
@ -1,4 +1,4 @@
|
|||
package algorithms
|
||||
package sort
|
||||
|
||||
import (
|
||||
"testing"
|
|
@ -1,4 +1,4 @@
|
|||
package algorithms
|
||||
package sort
|
||||
|
||||
import (
|
||||
"testing"
|
Loading…
Reference in New Issue