Esempio n. 1
0
func Merge(arr *array.List, left int, mid int, right int) {
	i := left
	j := mid + 1
	t := 0
	temp := make([]interface{}, right-left+1)
	for i <= mid && j <= right {
		res, err := arr.Compare(i, j)
		if err != nil {
			panic(fmt.Sprintf("error in compare: %v, index i: %d, index j: %d", err.Error(), i, j))
		}
		if res < 0 {
			temp[t], _ = arr.Get(i)
			i += 1
		} else {
			temp[t], _ = arr.Get(j)
			j += 1
		}
		t += 1
	}

	for i <= mid {
		temp[t], _ = arr.Get(i)
		i += 1
		t += 1
	}

	for j <= right {
		temp[t], _ = arr.Get(j)
		j += 1
		t += 1
	}

	arr.CopyRange(left, temp)
}
Esempio n. 2
0
func Partition(arr *array.List, left, right int) int {
	i := left - 1
	j := i + 1
	for j < right {
		if ret, _ := arr.Compare(j, right); ret < 0 {
			i += 1
			arr.Swap(i, j)
			j += 1
		} else {
			j += 1
		}
	}
	arr.Swap(right, i+1)
	return i + 1
}