Created
May 2, 2016 18:02
-
-
Save phedoreanu/a4453b16d6c5d2016defaeafa3cbfccb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func insertionSort(a []int, asc bool) { | |
for i := 1; i < len(a); i++ { | |
for j := i; j > 0; j-- { | |
switch asc { | |
case true: | |
if a[j] < a[j - 1] { | |
a[j], a[j - 1] = a[j - 1], a[j] | |
} | |
case false: | |
if a[j] > a[j - 1] { | |
a[j], a[j - 1] = a[j - 1], a[j] | |
} | |
} | |
} | |
} | |
} | |
func main() { | |
a := []int{5, 2, 6, 3, 1, 4} | |
b := []int{31, 41, 59, 26, 41, 58} | |
insertionSort(a, true) | |
fmt.Println(a) | |
insertionSort(a, false) | |
fmt.Println(a) | |
fmt.Println() | |
insertionSort(b, true) | |
fmt.Println(b) | |
insertionSort(b, false) | |
fmt.Println(b) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://play.golang.org/p/n7TB5HeLA9