Last active
July 29, 2018 21:08
-
-
Save squaredice/93f988802229e5986500decac7cf98ad to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
"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