diff --git a/go/sorting/gnomesort.go b/go/sorting/gnomesort.go new file mode 100644 index 000000000..0940b09ed --- /dev/null +++ b/go/sorting/gnomesort.go @@ -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) +} diff --git a/go/sorting/heapsort.go b/go/sorting/heapsort.go new file mode 100644 index 000000000..e1bd84138 --- /dev/null +++ b/go/sorting/heapsort.go @@ -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) +} diff --git a/go/sorting/mergesort.go b/go/sorting/mergesort.go new file mode 100644 index 000000000..b3986ce39 --- /dev/null +++ b/go/sorting/mergesort.go @@ -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) +} diff --git a/go/sorting/pancakesort.go b/go/sorting/pancakesort.go new file mode 100644 index 000000000..3a810dec3 --- /dev/null +++ b/go/sorting/pancakesort.go @@ -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) +} diff --git a/go/sorting/pigeonsort.go b/go/sorting/pigeonsort.go new file mode 100644 index 000000000..388e86092 --- /dev/null +++ b/go/sorting/pigeonsort.go @@ -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) +} diff --git a/go/sorting/selectionsort.go b/go/sorting/selectionsort.go new file mode 100644 index 000000000..6e71d8fe7 --- /dev/null +++ b/go/sorting/selectionsort.go @@ -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) +}