Example #1
0
func reverse(data sort.Interface, a, b int) {
	for a < b {
		data.Swap(a, b)
		a++
		b--
	}
}
Example #2
0
func isort(A sort.Interface, a, b int) {
	for j := a + 1; j < b; j++ {
		for i := j; i > a && A.Less(i, i-1); i-- {
			A.Swap(i, i-1)
		}
	}
}
Example #3
0
func Quicksort(sortable sort.Interface) {

	partition := func(sortable sort.Interface, lo, hi int) int {
		pivot := hi
		i := lo
		for j := lo; j < hi; j++ {
			if sortable.Less(j, pivot) {
				sortable.Swap(i, j)
				i++
			}
		}
		sortable.Swap(i, hi)
		return i
	}

	var quicksortRecursive func(sortable sort.Interface, lo, hi int)

	quicksortRecursive = func(sortable sort.Interface, lo, hi int) {
		if lo < hi {
			p := partition(sortable, lo, hi)
			quicksortRecursive(sortable, lo, p-1)
			quicksortRecursive(sortable, p+1, hi)
		}
	}

	quicksortRecursive(sortable, 0, sortable.Len()-1)

}
Example #4
0
// Insertion sort
func insertionSort(data sort.Interface, a, b int) {
	for i := a + 1; i < b; i++ {
		for j := i; j > a && data.Less(j, j-1); j-- {
			data.Swap(j, j-1)
		}
	}
}
// Simple insertion sort for smaller data collections.
func insertionSort(data sort.Interface, lo, hi int) {
	for i := lo + 1; i < hi+1; i++ {
		for j := i; j > lo && data.Less(j, j-1); j-- {
			data.Swap(j, j-1)
		}
	}
}
Example #6
0
// TimSort sorts the data defined by sort.Interface.
func TimSort(a sort.Interface) (err error) {
	indexes := make([]int, a.Len())
	for i := 0; i < len(indexes); i++ {
		indexes[i] = i
	} // for i

	err = Ints(indexes, func(i, j int) bool {
		return a.Less(i, j)
	})

	if err != nil {
		return err
	} // if

	for i := 0; i < len(indexes); i++ {
		j := indexes[i]
		if j == 0 {
			continue
		} //  if
		for k := i; j != i; {
			a.Swap(j, k)
			k, j, indexes[j] = j, indexes[j], 0
		} // for j
	} // for i

	return nil
}
Example #7
0
func xcopy(data sort.Interface, i, j, k, l int) int {
	for i < k && j < l {
		data.Swap(i, j)
		i, j = i+1, j+1
	}
	return i
}
Example #8
0
// InsertionSort performs a simple insertion sort on the sort interface. In the
// case of ByDist it performs generally as fast as sort.Sort() except that it
// can exploit temporal coherence improving performance dramatically when the
// objects have not moved much.
func InsertionSort(data sort.Interface) {
	for i := 0; i < data.Len(); i++ {
		for j := i; j > 0 && data.Less(j, j-1); j-- {
			data.Swap(j, j-1)
		}
	}
}
Example #9
0
// InsertionSort 插入排序,时间复杂度O(n^2)。
//
// 这个实现是拷贝的sort包下的 insertionSort()。
func InsertionSort(data sort.Interface, start, end int) {
	for i := start + 1; i < end; i++ {
		for j := i; j > start && data.Less(j, j-1); j-- {
			data.Swap(j, j-1)
		}
	}
}
Example #10
0
// partition the subarray a[lo .. hi] by returning an index j
// so that a[lo .. j-1] <= a[j] <= a[j+1 .. hi]
func partition(a sort.Interface, lo, hi int) int {
	i, j := lo+1, hi
	// v = a[lo]
	for {
		for a.Less(i, lo) {
			if i == hi {
				break
			}
			i++
		}
		for a.Less(lo, j) {
			if j == lo {
				break
			}
			j--
		}
		if i >= j {
			break
		}
		a.Swap(i, j)
		i++
		j--
	}
	// put v = a[j] into position
	a.Swap(lo, j)
	// with a[lo .. j-1] <= a[j] <= a[j+1 .. hi]
	return j
}
Example #11
0
// Partial quicksort algorithm due to Martínez (2004),
// http://www.cs.upc.edu/~conrado/research/reports/ALCOMFT-TR-03-50.pdf
func partialSort(data sort.Interface, k, lo, hi int) {
	for hi-lo > 5 {
		p := medianOfThree(data, lo, hi)
		p = partition(data, lo, hi, p)
		if p < k-1 {
			partialSort(data, k, p+1, hi)
		}
		hi = p
	}

	// Finish off with a selection sort.
	if hi-lo-1 < k {
		k = hi - lo - 1
	}
	for ; k > 0; k-- {
		min := lo
		for i := lo + 1; i < hi; i++ {
			if data.Less(i, min) {
				min = i
			}
		}
		data.Swap(lo, min)
		lo++
	}
}
Example #12
0
// HeapSort sorts given data and has next properties:
//
// - Not stable
// - O(1) extra space
// - O(n*lg(n)) time
// - Not really adaptive
//
func HeapSort(data sort.Interface) {
	// down restores order of heap.
	down := func(root, n int) {
		for {
			lch := 2*root + 1
			if lch >= n || lch < 0 { // child < 0 when int overflow.
				break
			}
			if rch := lch + 1; rch < n && data.Less(lch, rch) { // lch+1 == 2*root + 2 // Right child.
				lch = rch
			}
			if !data.Less(root, lch) { // Heap is ordered.
				return
			}
			data.Swap(root, lch)
			root = lch
		}
	}

	// Heapify (build a max heap).
	for i := (data.Len() - 1) / 2; i >= 0; i-- {
		down(i, data.Len())
	}

	// Pop elements, largest first, into end of data.
	// Loop invariant: data[i:] contains the data.Len()-1-i largest elements
	// of maxHeap and the maxHeap contains i+1 smallest elements.
	for i := data.Len() - 1; i >= 0; i-- {
		data.Swap(0, i)
		down(0, i)
	}
}
func insertionSort(a sort.Interface) {
	for i := 1; i < a.Len(); i++ {
		for j := i; j > 0 && a.Less(j, j-1); j-- {
			a.Swap(j-1, j)
		}
	}
}
Example #14
0
// Insertion sort
func insertionSort(data sort.Interface) {
	n := data.Len()
	for i := 1; i < n; i++ {
		for j := i; j > 0 && data.Less(j, j-1); j-- {
			data.Swap(j, j-1)
		}
	}
}
Example #15
0
// Insertion sort
func Insertion(a sort.Interface) {
	for i := 2; i < a.Len(); i++ {
		// insert a[j] into sorted slice a[0:j]
		for j := i; j > 0 && a.Less(j, j-1); j-- {
			a.Swap(j, j-1)
		}
	}
}
Example #16
0
func reverse(seq sort.Interface, firstIndex int) {
	lastIndex := seq.Len() - 1

	numSwap := (lastIndex - firstIndex + 1) / 2
	for i := 0; i < numSwap; i++ {
		seq.Swap(firstIndex+i, lastIndex-i)
	}
}
Example #17
0
func insertSort(data sort.Interface) {
	r := data.Len() - 1
	for i := 1; i <= r; i++ {
		for j := i; j > 0 && data.Less(j, j-1); j-- {
			data.Swap(j, j-1)
		}
	}
}
Example #18
0
// Proposition. For randomly ordered arrays of length N with with distinct keys,
// insertion sort uses ~N2/4 compares and ~N2/4 exchanges on the average.
// The worst case is ~ N2/2 compares and ~ N2/2 exchanges and the best case is N-1 compares and 0 exchanges.
func InsertionSort(a sort.Interface) {
	n := a.Len()
	for i := 1; i < n; i++ {
		for j := i; j > 0 && a.Less(j, j-1); j-- {
			a.Swap(j, j-1)
		}
	}
}
Example #19
0
// Shuffle sorts data in a randomized order. It uses the default
// Source in math/rand, so if clients want to manipulate the outcome,
// they should call the appropriate functions in math/rand.
//
// TODO: Add ability to use custom Sources.
func Shuffle(data sort.Interface) {
	length := data.Len()

	for i := 0; i < length; i++ {
		i2 := rand.Intn(i + 1)
		data.Swap(i, i2)
	}
}
Example #20
0
func Insertion(c sort.Interface) {
	var l = c.Len()
	for i := 1; i < l; i++ {
		for j := i; j > 0 && c.Less(j, j-1); j-- {
			c.Swap(j, j-1)
		}
	}
}
Example #21
0
// Flip reverses the order of items in a sort.Interface.
func Flip(data sort.Interface) {
	a, b := 0, data.Len()-1
	for b > a {
		data.Swap(a, b)
		a++
		b--
	}
}
func heapSort(a sort.Interface) {
	for start := (a.Len() - 2) / 2; start >= 0; start-- {
		siftDown(a, start, a.Len()-1)
	}
	for end := a.Len() - 1; end > 0; end-- {
		a.Swap(0, end)
		siftDown(a, 0, end-1)
	}
}
Example #23
0
func bubbleSort(data sort.Interface) {
	r := data.Len() - 1
	for i := 0; i < r; i++ {
		for j := r; j > i; j-- {
			if data.Less(j, j-1) {
				data.Swap(j, j-1)
			}
		}
	}
}
Example #24
0
// BubbleSort implementation
func Bubble(a sort.Interface) {

	for i := 0; i < a.Len(); i++ {
		for j := a.Len() - 1; j > i; j-- {
			if a.Less(j, j-1) {
				a.Swap(j, j-1)
			}
		}
	}
}
Example #25
0
func up(h sort.Interface, left int) {
	for {
		parent := (left - 1) / 2 // parent
		if parent == left || !h.Less(left, parent) {
			break
		}
		h.Swap(parent, left)
		left = parent
	}
}
Example #26
0
// InsertionSort sorts given data and has next properties:
//
// - Stable
// - O(1) extra space
// - O(n*n) comparisons and swaps
// - Adaptive: O(n) time when nearly sorted
// - Very low overhead
//
func InsertionSort(data sort.Interface) {
	// Loop invariant: at the start of each iteration, the data[0:i] consist
	// of the elements originally in data[0:i], but in sorted order.
	for i := 1; i < data.Len(); i++ {
		// Loop invariant: data[k:i] will have the smallest element on position k.
		for k := i; k > 0 && data.Less(k, k-1); k-- {
			data.Swap(k, k-1)
		}
	}
}
Example #27
0
func Heapsort(data sort.Interface) sort.Interface {
	buildMaxHeap(data)
	heapsize := data.Len()
	for i := data.Len() - 1; i > 0; i-- {
		data.Swap(0, i)
		heapsize--
		maxHeapify(data, 0, heapsize)
	}
	return data
}
Example #28
0
func SelectionSort(sortable sort.Interface) {
	for i := 0; i < sortable.Len(); i++ {
		smallest := i
		for j := i + 1; j < sortable.Len(); j++ {
			if sortable.Less(j, smallest) {
				smallest = j
			}
		}
		sortable.Swap(i, smallest)
	}
}
Example #29
0
//插入排序,兼容标准库排序接口
func Insertion(data sort.Interface) {
	for i := 1; i < data.Len(); i++ {
		for x := i - 1; x >= 0; x-- {
			if data.Less(x, x+1) {
				break
			} else {
				data.Swap(x, x+1)
			}
		}
	}
}
Example #30
0
// Bubble sort
func bubbleSort(data sort.Interface) {
	keepGoing := true
	for keepGoing {
		keepGoing = false
		for i := 0; i < data.Len()-1; i++ {
			if data.Less(i+1, i) {
				data.Swap(i, i+1)
				keepGoing = true
			}
		}
	}
}