Add insertion sort with less loops
This commit is contained in:
parent
1d68733dd8
commit
455d14ce69
@ -35,4 +35,18 @@ func InsertionSort(list []int) []int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return list
|
return list
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TheOtherInsertionSort(list []int) []int {
|
||||||
|
for round := 1; round < len(list); round++ {
|
||||||
|
element_to_insert := list[round]
|
||||||
|
pos := round
|
||||||
|
for pos > 0 && element_to_insert < list[pos-1] {
|
||||||
|
list[pos] = list[pos-1]
|
||||||
|
pos--
|
||||||
|
}
|
||||||
|
list[pos] = element_to_insert
|
||||||
|
}
|
||||||
|
return list
|
||||||
}
|
}
|
||||||
|
2
main.go
2
main.go
@ -1,7 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
import "clerie.de/letsgo/aud"
|
import "clerie.de/letsgo/aud"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
aud.InsertionSort([]int {2,6,1,3,5,4})
|
aud.InsertionSort([]int {2,6,1,3,5,4})
|
||||||
|
fmt.Println(aud.TheOtherInsertionSort([]int {2,6,1,3,5,4}))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user