Exemplo n.º 1
0
func InsertSort(toBeSorted interfaces.Swapable) {
	length := toBeSorted.Len()

	for i := 0; i < length-1; i++ {

		current := toBeSorted.Element(i + 1)
		j := i

		for ; j >= 0 && toBeSorted.Element(j).IsGreaterThan(current); j-- {
			toBeSorted.Swap(j+1, j)
		}
		toBeSorted.SetElement(j+1, current)
	}
}
Exemplo n.º 2
0
func BubbleSort(toBeSorted interfaces.Swapable) {
	length := toBeSorted.Len()

	for j := length - 1; 0 < j; j-- {
		for i := 0; i < j; i++ {
			if toBeSorted.Element(i).IsGreaterThan(toBeSorted.Element(i + 1)) {
				toBeSorted.Swap(i, i+1)
			}
		}
	}
}
Exemplo n.º 3
0
func SelectionSort(toBeSorted interfaces.Swapable) {
	length := toBeSorted.Len()

	for i := 0; i < length-1; i++ {

		minJ := i
		for j := i + 1; j < length; j++ {

			if toBeSorted.Element(minJ).IsGreaterThan(toBeSorted.Element(j)) {
				minJ = j
			}
		}

		if minJ != i {
			toBeSorted.Swap(i, minJ)
		}
	}
}