mirror of
https://github.com/clockworkpi/LauncherGoDev.git
synced 2026-03-25 05:12:49 +01:00
add draw rect and new surface
This commit is contained in:
37
gogame/qsort/qsort.go
Normal file
37
gogame/qsort/qsort.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package qsort
|
||||
|
||||
import "math/rand"
|
||||
|
||||
func QuickSort(slice []int) []int {
|
||||
length := len(slice)
|
||||
|
||||
if length <= 1 {
|
||||
sliceCopy := make([]int, length)
|
||||
copy(sliceCopy, slice)
|
||||
return sliceCopy
|
||||
}
|
||||
|
||||
m := slice[rand.Intn(length)]
|
||||
|
||||
less := make([]int, 0, length)
|
||||
middle := make([]int, 0, length)
|
||||
more := make([]int, 0, length)
|
||||
|
||||
for _, item := range slice {
|
||||
switch {
|
||||
case item < m:
|
||||
less = append(less, item)
|
||||
case item == m:
|
||||
middle = append(middle, item)
|
||||
case item > m:
|
||||
more = append(more, item)
|
||||
}
|
||||
}
|
||||
|
||||
less, more = QuickSort(less), QuickSort(more)
|
||||
|
||||
less = append(less, middle...)
|
||||
less = append(less, more...)
|
||||
|
||||
return less
|
||||
}
|
||||
27
gogame/qsort/qsort_test.go
Normal file
27
gogame/qsort/qsort_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package qsort
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var testData = []struct {
|
||||
input []int
|
||||
expectedOutput []int
|
||||
}{
|
||||
{[]int{}, []int{}},
|
||||
{[]int{42}, []int{42}},
|
||||
{[]int{42, 23}, []int{23, 42}},
|
||||
{[]int{23, 42, 32, 64, 12, 4}, []int{4, 12, 23, 32, 42, 64}},
|
||||
}
|
||||
|
||||
func TestQuickSort(t *testing.T) {
|
||||
for _, testCase := range testData {
|
||||
actual := QuickSort(testCase.input)
|
||||
expected := testCase.expectedOutput
|
||||
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("%v != %v\n", actual, expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user