From 1a24822b711ab13356c3227ce61d8ed71ac437df Mon Sep 17 00:00:00 2001 From: Olivia Schwartz <147622457+oliviasch@users.noreply.github.com> Date: Sun, 22 Sep 2024 14:40:56 -0400 Subject: [PATCH 1/6] issue 366: heapsort implementation --- go/sorting/heapsort.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 go/sorting/heapsort.go 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) +} From 7412a4572d4b3d5dd89f9a4fcdd4d718055fea03 Mon Sep 17 00:00:00 2001 From: Olivia Schwartz <147622457+oliviasch@users.noreply.github.com> Date: Sun, 22 Sep 2024 17:42:33 -0400 Subject: [PATCH 2/6] issue 366: mergesort implementation --- go/sorting/mergesort.go | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 go/sorting/mergesort.go 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) +} From e89a8f49cb9cbae273ad1235b147e70df01c1fa9 Mon Sep 17 00:00:00 2001 From: Olivia Schwartz <147622457+oliviasch@users.noreply.github.com> Date: Sun, 22 Sep 2024 18:02:23 -0400 Subject: [PATCH 3/6] issue 366: selectionsort implementation --- go/sorting/selectionsort.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 go/sorting/selectionsort.go 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) +} From 81c32f08d8d5c430ee9ecb4c0f740a8712d4deb7 Mon Sep 17 00:00:00 2001 From: Olivia Schwartz <147622457+oliviasch@users.noreply.github.com> Date: Sun, 22 Sep 2024 19:37:10 -0400 Subject: [PATCH 4/6] issue 366: gnomesort implementation --- go/sorting/gnomesort.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 go/sorting/gnomesort.go 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) +} From d1dd5f36ae6db4e2a6ac1f283cb602e54bb8e075 Mon Sep 17 00:00:00 2001 From: Olivia Schwartz <147622457+oliviasch@users.noreply.github.com> Date: Mon, 23 Sep 2024 18:56:39 -0400 Subject: [PATCH 5/6] issue 366: pancakesort implementation --- go/sorting/pancakesort.go | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 go/sorting/pancakesort.go 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) +} From 595a3df870b7455443d3358c2da135c1b701e0cf Mon Sep 17 00:00:00 2001 From: Olivia Schwartz <147622457+oliviasch@users.noreply.github.com> Date: Thu, 26 Sep 2024 21:08:04 -0400 Subject: [PATCH 6/6] issue 366: pigeonsort implementation --- go/sorting/pigeonsort.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 go/sorting/pigeonsort.go 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) +}