Exemplo n.º 1
0
func partition(arr []int, left, right, pivotIdx int) int {
	var idx, store int
	var pivot int = arr[pivotIdx]

	// fmt.Printf("pivotIdx: %d\n", pivotIdx)
	// fmt.Printf("pivot: %d\n", pivot)

	var tmp int = arr[right]
	arr[right] = arr[pivotIdx]
	arr[pivotIdx] = tmp
	// fmt.Println(arr)

	store = left
	for idx = left; idx < right; idx++ {
		if helper.Compare(arr[idx], pivot) <= 0 {
			// fmt.Println(arr[idx], pivot, helper.Compare(arr[idx], pivot))
			tmp = arr[idx]
			arr[store] = tmp
			// fmt.Println(arr)
			store++
		}
	}

	tmp = arr[right]
	arr[right] = arr[store]
	arr[store] = tmp

	// fmt.Println(arr)
	// fmt.Println(store)
	return store
}
Exemplo n.º 2
0
func sort(arr []int) []int {
	var max int = len(arr)
	for i := 0; i < max; i++ {
		for k := i; k < max; k++ {
			if helper.Compare(arr[i], arr[k]) > 0 {
				var tmp int = arr[i]
				arr[i] = arr[k]
				arr[k] = tmp
			}
		}
	}
	return arr
}
Exemplo n.º 3
0
func sort(arr []int) []int {
	max := len(arr)

	for i := 1; i < max; i++ {
		for index := 0; index < i; index++ {
			if helper.Compare(arr[index], arr[i]) > 0 {
				tmp := arr[index]
				arr[index] = arr[i]
				arr[i] = tmp
			}
		}
	}

	return arr
}
Exemplo n.º 4
0
func sort(arr []int) []int {
	left := 0
	max := len(arr) - 1

	for index := 0; index < max; index++ {
		for k := max; k > left; k-- {
			if helper.Compare(arr[k-1], arr[k]) > 0 {
				tmp := arr[k-1]
				arr[k-1] = arr[k]
				arr[k] = tmp
			}
		}
		left++
	}
	return arr
}