Skip to content

Commit

Permalink
Merge pull request #17 from fabiothiroki/insertion-sort
Browse files Browse the repository at this point in the history
add insertion sort
  • Loading branch information
fabiothiroki committed Apr 21, 2021
2 parents 68bfbdf + ac7f093 commit c1a1c29
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions sort/insertionsort/inserstionsort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package insertionsort

// InsertionSort:
// Average/Worst Time: O(n^2) / Space: O(1)
// Best: time O(n) space O(1)
func InsertionSort(array []int) []int {

for i := range array {
for j := i; j > 0 && array[j-1] > array[j]; j-- {
array[j-1], array[j] = array[j], array[j-1]
}
}

return array
}
14 changes: 14 additions & 0 deletions sort/insertionsort/insertionsort_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package insertionsort

import (
"reflect"
"testing"
)

func TestOne(t *testing.T) {
r := InsertionSort([]int{9, 8, 7, 6})

if !reflect.DeepEqual(r, []int{6, 7, 8, 9}) {
t.Errorf("Got: %v, want: %v.", r, []int{6, 7, 8, 9})
}
}

0 comments on commit c1a1c29

Please sign in to comment.