Skip to content

Instantly share code, notes, and snippets.

@trigun117
Last active July 29, 2018 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trigun117/93f988802229e5986500decac7cf98ad to your computer and use it in GitHub Desktop.
Save trigun117/93f988802229e5986500decac7cf98ad to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"time"
)
// Bubble sorting
func Bubble(slice []int) {
t := time.Now()
for i := 0; i < len(slice); i++ {
for j := 0; j < len(slice)-1-i; j++ {
if slice[j] > slice[j+1] {
slice[j], slice[j+1] = slice[j+1], slice[j]
}
}
}
fmt.Println("Bubble: ", time.Since(t))
}
// Insertion sorting
func Insertion(slice []int) {
t := time.Now()
for i := 0; i < len(slice); i++ {
j := i
for j > 0 && slice[j] < slice[j-1] {
slice[j], slice[j-1] = slice[j-1], slice[j]
j--
}
}
fmt.Println("Insertion: ", time.Since(t))
}
// GenSLICE create slice with random integer values
func GenSLICE(num int) []int {
var slice []int
for num > 0 {
slice = append(slice, rand.Intn(1000))
num--
}
return slice
}
// IsPalindrome check if string is a palindrome
func IsPalindrome(str string) bool {
for i := 0; i < len(str)/2; i++ {
if str[i] != str[len(str)-i-1] {
return false
}
}
return true
}
// FizzBuzz reproduce FizzBuzz exercise
func FizzBuzz() {
for i := 1; i <= 100; i++ {
switch {
case i%15 == 0:
fmt.Println("FizzBuzz")
case i%3 == 0:
fmt.Println("Fizz")
case i%5 == 0:
fmt.Println("Buzz")
default:
fmt.Println(i)
}
}
}
func main() {
// Sorting
/*rand.Seed(time.Now().UnixNano())
slice := GenSLICE(100000)
slice1 := GenSLICE(100000)
go Bubble(slice)
go Insertion(slice1)
var n int
fmt.Scan(&n)*/
// Palindrome
/*var i string
fmt.Scan(&i)
fmt.Println(IsPalindrome(i))*/
// FizzBuzz
//FizzBuzz()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment