1
0
Fork 0

Implemented shuffle sort algorithm

This commit is contained in:
Gregory Eremin 2015-10-12 01:19:56 +03:00
parent 0357c86f7d
commit b929919bfb
No known key found for this signature in database
GPG Key ID: 5EFA427EEC26E86C
2 changed files with 41 additions and 0 deletions

29
sort/shuffle_sort.go Normal file
View File

@ -0,0 +1,29 @@
package sort
import (
"math/rand"
)
// ShuffleSort is an implementation of shuffle sort algorithm.
func ShuffleSort(a []int) []int {
isSorted := func(a []int) bool {
for i := 1; i < len(a); i++ {
if a[i-1] > a[i] {
return false
}
}
return true
}
for {
if isSorted(a) {
return a
}
for i := range a {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
}
}

12
sort/shuffle_sort_test.go Normal file
View File

@ -0,0 +1,12 @@
package sort
import (
"testing"
)
func TestShuffleSort(t *testing.T) {
testSort(t, ShuffleSort,
[]int{5, 3, 1, 4, 2},
[]int{1, 2, 3, 4, 5},
)
}