Exemple #1
0
func partition(data sortable.Interface, start, end int) int {
	p, i, j := start, start, end+1
	for true {
		i = i + 1
		for data.Less(i, p) {
			if i >= end {
				break
			}
			i = i + 1
		}
		j = j - 1
		for data.Less(p, j) {
			if j <= start {
				break
			}
			j = j - 1
		}

		if i >= j {
			break
		}
		data.Swap(i, j)
	}
	data.Swap(p, j)
	return j
}
Exemple #2
0
func Sort(data sortable.Interface) {
	for i := 0; i < data.Len()-1; i++ {
		for j := i + 1; j > 0 && data.Less(j, j-1); j-- {
			data.Swap(j, j-1)
		}
	}
}
Exemple #3
0
func Sort(data sortable.Interface) {
	for i := 0; i < data.Len(); i++ {
		min := i
		for j := i + 1; j < data.Len(); j++ {
			if data.Less(j, min) {
				min = j
			}
		}
		data.Swap(i, min)
	}
}
Exemple #4
0
func Sort(data sortable.Interface) {
	n := data.Len() - 1
	for i := n / 2; i >= 0; i-- {
		sink(data, i, n)
	}

	for n > 0 {
		data.Swap(0, n)
		n = n - 1
		sink(data, 0, n)
	}
}
Exemple #5
0
func sink(data sortable.Interface, curr, total int) {
	for curr*2+1 <= total {
		c := curr*2 + 1
		if c+1 <= total && data.Less(c, c+1) {
			c = c + 1
		}
		if !data.Less(curr, c) {
			break
		}
		data.Swap(curr, c)
		curr = c
	}
}
Exemple #6
0
func Sort(data sortable.Interface) {
	h := 1
	for h < data.Len()/3 {
		h = 3*h + 1
	}

	for h >= 1 {
		for i := 0; i < data.Len()-h; i = i + h {
			for j := i + h; j > 0 && data.Less(j, j-h); j = j - h {
				data.Swap(j, j-1)
			}
		}
		h = h / 3
	}
}
Exemple #7
0
func quickSort3Way(data sortable.Interface, start, end int) {
	if start >= end {
		return
	}
	v, lt, i, gt := start, start+1, start+1, end
	for i <= gt {
		if data.Less(i, v) {
			data.Swap(i, lt)
			lt = lt + 1
			i = i + 1
		} else if data.Less(v, i) {
			data.Swap(i, gt)
			gt = gt - 1
		} else {
			i = i + 1
		}
	}
	data.Swap(v, lt-1)
	quickSort3Way(data, start, lt-1)
	quickSort3Way(data, gt+1, end)
}