Gnome Sort Algorithm

Gnome sort (dubbed stupid sort) is a sorting algorithm originally proposed by an Iranian computer scientist Hamid Sarbazi-Azad (professor of computer Science and technology at Sharif University of technology) in 2000.The sort was first named stupid sort (not to be confused with bogosort), and then later described by Dick Grune and named gnome sort.
package GnomeSort

import (
	"math/rand"
	"sort"
	"testing"
	"time"
)


func GnomeSort(array []int) {
	itemIndex := 0
	for itemIndex < len(array)-1 {
		if array[itemIndex] > array[itemIndex+1] {
			array[itemIndex], array[itemIndex+1] = array[itemIndex+1], array[itemIndex]
			if itemIndex != 0 {
				itemIndex -= 1
			}
		} else {
			itemIndex += 1
		}
	}
}

func TestGnomeSort(t *testing.T) {
	random := rand.New(rand.NewSource(time.Now().UnixNano()))
	array1 := make([]int, random.Intn(100-10)+10)
	for i := range array1 {
		array1[i] = random.Intn(100)
	}
	array2 := make(sort.IntSlice, len(array1))
	copy(array2, array1)
	GnomeSort(array1)
	array2.Sort()
	for i := range array1 {
		if array1[i] != array2[i] {
			t.Fail()
		}
	}
}

LANGUAGE:

DARK MODE: