Beispiel #1
0
func main() {
	nums := common.GenerateRandomNumbers(1000000, 10, 100000)
	delegate := &common.PrintDelegate{}
	// ExecuteSort(BubbleSort, nums, delegate)
	// ExecuteSort(SelectionSort, nums, delegate)
	// ExecuteSort(InsertionSort, nums, delegate)
	// ExecuteSort(ShellSort, nums, delegate)
	ExecuteSort(QuickSort, nums, delegate)
	ExecuteSort(MergeSort, nums, delegate)
}
func Test_LomutoPartition(t *testing.T) {
	slice := common.GenerateRandomNumbers(100, 1, 100)
	l := 13
	r := len(slice) - 7
	pivot := slice[l]
	t.Log("slice", slice)
	s := LomutoPartition(slice, l, r)
	t.Log("partitioned", slice)
	for i := l; i <= r; i++ {
		a := slice[i]
		if i < s && a >= pivot || i == s && a != pivot || i > s && a < pivot {
			t.Error("Wrong partition:", i, a)
		}
	}
}
func Test_QuickSelect(t *testing.T) {
	slice := common.GenerateRandomNumbers(50, 1, 100)
	// slice := common.GenerateNumbers(50)
	k := 31

	sorted := sorting.ExecuteSort(sorting.ShellSort, slice, nil)
	kth := sorted[k-1]

	t.Logf("finding %dth\n", k)
	i, n := QuickSelect(slice, k)
	t.Log("slice after", slice)
	t.Log("i", i, "n", n)
	if n != kth {
		t.Error("Wrong selection")
	}
}
func appMain(driver gxui.Driver) {
	theDriver = driver
	theme = flags.CreateTheme(driver)

	window := theme.CreateWindow(winW, winH, "Window")
	window.OnClose(driver.Terminate)
	window.SetScale(flags.DefaultScaleFactor)

	layout := theme.CreateLinearLayout()
	layout.SetBackgroundBrush(gxui.CreateBrush(gxui.Black))
	layout.SetDirection(gxui.LeftToRight)
	layout.SetVerticalAlignment(gxui.AlignBottom)

	layout.SetSizeMode(gxui.Fill)

	nums := common.GenerateRandomNumbers(numBars, 0, valNum)
	for _, n := range nums {
		child := createBar()
		setBarHeight(child, n)
		layout.AddChild(child)
		bars = append(bars, child)
	}

	window.AddChild(layout)

	delegate := &GUIDelegate{}

	go func() {
		<-time.After(1 * time.Second)
		fmt.Println("ExecuteSort...")
		// sorting.ExecuteSort(sorting.InsertionSort, nums, delegate)
		// sorting.ExecuteSort(sorting.BubbleSort, nums, delegate)
		// sorting.ExecuteSort(sorting.SelectionSort, nums, delegate)
		// sorting.ExecuteSort(sorting.ShellSort, nums, delegate)
		// sorting.ExecuteSort(sorting.MergeSort, nums, delegate)
		sorting.ExecuteSort(sorting.QuickSort, nums, delegate)
		// fmt.Println(result)
	}()
}