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