insertion sort


See On Github

Data

Contributor

Generic placeholder thumbnail

by jcla1

in go

Source Code

package insertionSort

// Works in-place
// Avg time complexity: O(n^2)
func InsertionSort(arr []int) []int {
	for i := 1; i < len(arr); i++ {
		for j := i; j > 0; j-- {
			if arr[j] < arr[j-1] {
				// swap the elements
				arr[j], arr[j-1] = arr[j-1], arr[j]
			} else {
				break
			}
		}
	}

	return arr
}
package insertionSort

import "testing"

func TestInsertionSort(t *testing.T) {
	arrs := [][]int{[]int{-2, 1, -3, 4, -1, 2, 1, -5, 4}, []int{2, 3, 7, -5, -1, 4, -10}}

	var result []int

	for _, arr := range arrs {
		result = InsertionSort(arr)
		if !isSorted(result) {
			t.Errorf("InsertionSort(%v) = %v", arr, result)
		}
	}
}

func isSorted(arr []int) bool {
	for i := 1; i < len(arr); i++ {
		if arr[i-1] > arr[i] {
			return false
		}
	}

	return true
}