Ejemplo n.º 1
0
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")
	}
}
Ejemplo n.º 2
0
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)
	}()
}