From eac737c7382cd31e6b2fb1b67dd8cc2ff3191b4e Mon Sep 17 00:00:00 2001 From: Christopher Milan Date: Tue, 4 Sep 2018 15:53:15 -0400 Subject: [PATCH 1/3] Add bogo sort in Golang --- CONTRIBUTORS.md | 2 ++ contents/bogo_sort/code/go/main.go | 39 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 contents/bogo_sort/code/go/main.go diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 357a01ef0..17d1c0718 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -61,3 +61,5 @@ Bendik Samseth Trashtalk
Cyrus Burt +
+Christopher Milan diff --git a/contents/bogo_sort/code/go/main.go b/contents/bogo_sort/code/go/main.go new file mode 100644 index 000000000..2fe217837 --- /dev/null +++ b/contents/bogo_sort/code/go/main.go @@ -0,0 +1,39 @@ +// Submitted by Christopher Milan (christopherm99) + +package main + +import ( + "fmt" + "math/rand" + "time" +) + +func shuffle(a []int) []int { + rand.Seed(time.Now().UnixNano()) + for i := len(a) - 1; i > 0; i-- { + j := rand.Intn(i + 1) + a[i], a[j] = a[j], a[i] + } + return a +} + +func is_sorted(a []int) bool { + for i := 0; i < len(a)-1; i++ { + if a[i+1] < a[i] { + return false + } + } + return true +} + +func bogo_sort(a *[]int) { + for !is_sorted(*a) { + *a = shuffle(*a) + } +} + +func main() { + a := []int{1, 3, 4, 2} + bogo_sort(&a) + fmt.Println(a) +} From bb6b4cda0ff74856f32ecfc7272064880fa0caea Mon Sep 17 00:00:00 2001 From: Christopher Milan Date: Wed, 17 Oct 2018 07:25:45 -0400 Subject: [PATCH 2/3] Resolve Liikt's PR review comments. --- contents/bogo_sort/code/go/main.go | 45 +++++++++++++++--------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/contents/bogo_sort/code/go/main.go b/contents/bogo_sort/code/go/main.go index 2fe217837..e94981e64 100644 --- a/contents/bogo_sort/code/go/main.go +++ b/contents/bogo_sort/code/go/main.go @@ -3,37 +3,36 @@ package main import ( - "fmt" - "math/rand" - "time" + "fmt" + "math/rand" + "time" ) -func shuffle(a []int) []int { - rand.Seed(time.Now().UnixNano()) - for i := len(a) - 1; i > 0; i-- { - j := rand.Intn(i + 1) - a[i], a[j] = a[j], a[i] +func shuffle(a *[]int) { + for i := len(*a) - 1; i > 0; i-- { + j := rand.Intn(i + 1) + (*a)[i], (*a)[j] = (*a)[j], (*a)[i] } - return a } -func is_sorted(a []int) bool { - for i := 0; i < len(a)-1; i++ { - if a[i+1] < a[i] { - return false - } - } - return true +func isSorted(a []int) bool { + for i := 0; i < len(a)-1; i++ { + if a[i+1] < a[i] { + return false + } + } + return true } -func bogo_sort(a *[]int) { - for !is_sorted(*a) { - *a = shuffle(*a) - } +func bogoSort(a *[]int) { + for !isSorted(*a) { + shuffle(a) + } } func main() { - a := []int{1, 3, 4, 2} - bogo_sort(&a) - fmt.Println(a) + rand.Seed(time.Now().UnixNano()) + a := []int{1, 3, 4, 2} + bogoSort(&a) + fmt.Println(a) } From 0c14af1970c6b853e2cf5ddd77c020554af44ea4 Mon Sep 17 00:00:00 2001 From: Christopher Milan Date: Thu, 18 Oct 2018 08:22:50 -0400 Subject: [PATCH 3/3] Changed _all_ tabs to spaces --- contents/bogo_sort/code/go/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/bogo_sort/code/go/main.go b/contents/bogo_sort/code/go/main.go index e94981e64..a8297fc50 100644 --- a/contents/bogo_sort/code/go/main.go +++ b/contents/bogo_sort/code/go/main.go @@ -12,7 +12,7 @@ func shuffle(a *[]int) { for i := len(*a) - 1; i > 0; i-- { j := rand.Intn(i + 1) (*a)[i], (*a)[j] = (*a)[j], (*a)[i] - } + } } func isSorted(a []int) bool {