bogosort


See On Github

Data

Contributor

Generic placeholder thumbnail

by jcla1

in go

Source Code

package bogosort

import "math/rand"

func Bogosort(arr []int) []int {
	for !isSorted(arr) {
		fisherYates(arr)
	}

	return arr
}

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

	return true
}

// This function is just for shuffling,
// not part of the actual algorithm
func fisherYates(arr []int) []int {
	m := len(arr)
	var i int

	for m != 0 {
		i = rand.Intn(m)
		m -= 1
		arr[i], arr[m] = arr[m], arr[i]
	}

	return arr
}
package bogosort

import "testing"

func TestIsSorted(t *testing.T) {
	arrs := [][]int{[]int{1, 2, 3, 4}, []int{}, []int{4, 6, 1, 6, 8}}
	expectedResults := []bool{true, true, false}

	var result bool

	for i, expected := range expectedResults {
		result = isSorted(arrs[i])
		if result != expected {
			t.Errorf("isSorted(%v) = %t, want %t", arrs[i], result, expected)
		}
	}
}

func TestBogosort(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 = Bogosort(arr)
		if !isSorted(result) {
			t.Errorf("Bogosort(%v) = %v", arr, result)
		}
	}
}