summaryrefslogtreecommitdiff
path: root/sort/insertion.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2020-07-14 08:53:17 +0100
committerPaul Buetow <paul@buetow.org>2020-07-14 08:53:17 +0100
commit554a7a259f3feefb586374ac5a3b57acc99a4446 (patch)
tree33ad06e30abb352609dd4f75306504f6548caec8 /sort/insertion.go
parent7f0751cad2196932f16f753fdaf53bc3eea670b8 (diff)
add insertion sort
Diffstat (limited to 'sort/insertion.go')
-rw-r--r--sort/insertion.go22
1 files changed, 22 insertions, 0 deletions
diff --git a/sort/insertion.go b/sort/insertion.go
new file mode 100644
index 0000000..d14c275
--- /dev/null
+++ b/sort/insertion.go
@@ -0,0 +1,22 @@
+package sort
+
+import (
+ "algorithms/ds"
+)
+
+func Insertion(a []ds.Comparer) []ds.Comparer {
+ length := len(a)
+
+ for i := 0; i < length; i++ {
+ for j := i; j > 0; j-- {
+ if a[j].HigherThan(a[j-1]) {
+ break
+ }
+ tmp := a[j]
+ a[j] = a[j-1]
+ a[j-1] = tmp
+ }
+ }
+
+ return a
+}