Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting implementations in Go #593

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions go/sorting/gnomesort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import "fmt"

func gnomeSort(arr []int) {
i, n := 0, len(arr)
for i < n {
if i == 0 {
i++
}
if arr[i] >= arr[i-1] {
i++
} else {
arr[i], arr[i-1] = arr[i-1], arr[i]
i--
}
}
}

func main() {
temp := []int{14, 67, 22, 93, 8, 45, 31, 76, 54, 18}
gnomeSort(temp)
fmt.Println(temp)
}
36 changes: 36 additions & 0 deletions go/sorting/heapsort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import "fmt"

func heapify(arr []int, n int, index int) {
largest := index
left := 2*index + 1
right := 2*index + 2
if left < n && arr[left] > arr[largest] { // left child > root
largest = left
}
if right < n && arr[right] > arr[largest] { // right child > largest
largest = right
}
if largest != index { // largest not root
arr[index], arr[largest] = arr[largest], arr[index]
heapify(arr, n, largest)
}
}

func heapSort(arr []int) {
n := len(arr)
for i := n/2 - 1; i >= 0; i-- {
heapify(arr, n, i) // build heap inplace
}
for i := n - 1; i > 0; i-- {
arr[0], arr[i] = arr[i], arr[0]
heapify(arr, i, 0) // extract elements from heap
}
}

func main() {
temp := []int{42, 17, 88, 3, 56, 29, 74, 91, 10, 65}
heapSort(temp)
fmt.Println(temp)
}
57 changes: 57 additions & 0 deletions go/sorting/mergesort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import "fmt"

func merge(arr []int, left int, mid int, right int) {
// two subarrays - arr[left:mid+1] and arr[mid+1:right+1]
n1 := mid - left + 1
n2 := right - mid
l := make([]int, n1) // left slice
r := make([]int, n2) // right slice
// copies data into slices
for i := 0; i < n1; i++ {
l[i] = arr[left+i]
}
for j := 0; j < n2; j++ {
r[j] = arr[mid+1+j]
}
i, j, k := 0, 0, left
for i < len(l) && j < len(r) {
if l[i] <= r[j] {
arr[k] = l[i]
i++
} else {
arr[k] = r[j]
j++
}
k++
}
for i < len(l) { // copies remaining left elements
arr[k] = l[i]
i++
k++
}
for j < len(r) { // copies remaining right elements
arr[k] = r[j]
j++
k++
}
}

func mergeSortHelper(arr []int, left int, right int) {
if left >= right {
return
}
mid := left + (right-left)/2
mergeSortHelper(arr, left, mid)
mergeSortHelper(arr, mid+1, right)
merge(arr, left, mid, right)
}

func mergeSort(arr []int) { mergeSortHelper(arr, 0, len(arr)-1) }

func main() {
temp := []int{27, 3, 45, 18, 92, 56, 12, 78, 34, 81, 66, 24, 9, 43, 71}
mergeSort(temp)
fmt.Println(temp)
}
42 changes: 42 additions & 0 deletions go/sorting/pancakesort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import "fmt"

func flip(arr []int, i int) {
temp, start := 0, 0
for start < i { // flips elements from start to index i
temp = arr[start]
arr[start] = arr[i]
arr[i] = temp
start++
i--
}
}

func max(arr []int, size int) int {
max := 0
for i := 0; i < size; i++ {
if arr[i] > arr[max] {
max = i // max index
}
}
return max
}

func pancakeSort(arr []int) {
for n := len(arr); n > 1; n-- {
max := max(arr, n)
if max != n-1 {
flip(arr, max) // flips max to first position
flip(arr, n-1) // flips max to last position
}
}
}

func main() {
temp := []int{12, 45, 78, 23, 56, 89, 34, 67, 90, 11, 25, 38,
72, 94, 53, 61, 84, 29, 17, 66, 40, 82, 99, 13, 58, 74, 36,
92, 19, 47}
pancakeSort(temp)
fmt.Println(temp)
}
33 changes: 33 additions & 0 deletions go/sorting/pigeonsort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import "fmt"

func pigeonSort(arr []int) {
min, max := arr[0], arr[0]
for _, value := range arr { // finds max and min of slice
if value < min {
min = value
} else if value > max {
max = value
}
}
r := max - min + 1
holes := make([][]int, r) // creates holes
for i := 0; i < len(arr); i++ {
holes[arr[i]-min] = append(holes[arr[i]-min], arr[i])
}
index := 0
for i := 0; i < r; i++ { // puts holes into original slice
for _, value := range holes[i] {
arr[index] = value
index++
}
}
}

func main() {
temp := []int{47, 82, 19, 63, 28, 94, 15, 76, 54, 33,
88, 6, 72, 51, 98, 14, 39, 60, 25, 91, 3, 69, 22}
pigeonSort(temp)
fmt.Println(temp)
}
24 changes: 24 additions & 0 deletions go/sorting/selectionsort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import "fmt"

func selectionSort(arr []int) {
n := len(arr)
for i := 0; i < n-1; i++ {
min := i
for j := i + 1; j < n; j++ {
if arr[j] < arr[min] {
min = j
}
}
if min != i {
arr[min], arr[i] = arr[i], arr[min]
}
}
}

func main() {
temp := []int{42, 7, 19, 88, 53, 26, 91, 34, 75, 61}
selectionSort(temp)
fmt.Println(temp)
}