Insertion Sort: Go

This post shows an implementation of insertion sort written in Go. Insertion sort builds a sorted array at the beginning of the original array, iterating through the unsorted portion to add another element to the sorted portion with each pass. For a more detailed walkthrough, see my original post with an implementation in C.

func insertionSort(arr []int) {
    arrLen := len(arr)
    for i := 1; i < arrLen; i++ {
        key := arr[i]
        j := i - 1
        for j >= 0 && arr[j] > key {
            arr[j+1] = arr[j]
            j--
        }
        arr[j+1] = key
    }
}

Selection Sort: C

Next up in my “learning in public” push is selection sort, and an implementation in C. Like insertion sort, selection sort sorts the array in place, starting with a sorted beginning portion and an unsorted end. We iterate through the unsorted portion, and find the smallest value. We then swap that element with the first element in the unsorted portion. From there, the unsorted portion begins one index later, and we repeat the process until the entire array is sorted.

[Read More]

Insertion Sort: C

In my first post in my “learning in public” push, I’m going to be talking about the insertion sort algorithm and an implemention in C. The general idea of insertion sort is to build a sorted array in place, at the beginning of the original array, shifting the unsorted elements toward the end of the array as necessary. It relies on the fact that an array of length 1 is sorted by definition. [Read More]