├── go.mod ├── insertionsort └── insertionsort.go ├── selectionsort └── selectionsort.go ├── bubblesort └── bubblesort.go ├── quicksort └── quicksort.go ├── mergesort └── mergesort.go └── Readme.md /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/TutorialEdge/go-generic-algorithms 2 | 3 | go 1.16 4 | -------------------------------------------------------------------------------- /insertionsort/insertionsort.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Number interface { 6 | int | int8 | int16 | int32 | int64 | 7 | float32 | float64 8 | } 9 | 10 | func InsertionSort[number Number](input []number) []number { 11 | var n = len(input) 12 | for i := 1; i < n; i++ { 13 | j := i 14 | for j > 0 { 15 | if input[j-1] > input[j] { 16 | input[j-1], input[j] = input[j], input[j-1] 17 | } 18 | j = j - 1 19 | } 20 | } 21 | return input 22 | } 23 | 24 | func main() { 25 | list := []int32{4,3,1,5,} 26 | list2 := []float64{4.3, 5.2, 10.5, 1.2, 3.2,} 27 | 28 | sorted := InsertionSort(list) 29 | fmt.Println(sorted) 30 | 31 | sortedFloats := InsertionSort(list2) 32 | fmt.Println(sortedFloats) 33 | } -------------------------------------------------------------------------------- /selectionsort/selectionsort.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Number interface { 6 | int | int8 | int16 | int32 | int64 | 7 | float32 | float64 8 | } 9 | 10 | func SelectionSort[number Number](input []number) []number { 11 | var n = len(input) 12 | for i := 0; i < n; i++ { 13 | var minIndex = i 14 | for j := i; j < n; j++ { 15 | if input[j] < input[minIndex] { 16 | minIndex = j 17 | } 18 | } 19 | input[i], input[minIndex] = input[minIndex], input[i] 20 | } 21 | return input 22 | } 23 | 24 | func main() { 25 | list := []int32{4,3,1,5,} 26 | list2 := []float64{4.3, 5.2, 10.5, 1.2, 3.2,} 27 | 28 | 29 | sorted := SelectionSort(list) 30 | fmt.Println(sorted) 31 | sortedFloats := SelectionSort(list2) 32 | fmt.Println(sortedFloats) 33 | 34 | } -------------------------------------------------------------------------------- /bubblesort/bubblesort.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | 6 | type Number interface { 7 | int16 | int32 | int64 | float32 | float64 8 | } 9 | 10 | func BubbleSortGeneric[N Number](input []N) []N { 11 | n := len(input) 12 | swapped := true 13 | for swapped { 14 | swapped = false 15 | for i := 0; i < n-1; i++ { 16 | if input[i] > input[i+1] { 17 | input[i], input[i+1] = input[i+1], input[i] 18 | swapped = true 19 | } 20 | } 21 | } 22 | return input 23 | } 24 | 25 | 26 | func main() { 27 | fmt.Println("Go Generics Tutorial") 28 | list := []int32{4,3,1,5,} 29 | list2 := []float64{4.3, 5.2, 10.5, 1.2, 3.2,} 30 | sorted := BubbleSortGeneric(list) 31 | fmt.Println(sorted) 32 | 33 | sortedFloats := BubbleSortGeneric(list2) 34 | fmt.Println(sortedFloats) 35 | 36 | } 37 | -------------------------------------------------------------------------------- /quicksort/quicksort.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Number interface { 6 | int16 | int32 | int64 | float32 | float64 7 | } 8 | 9 | func Partition[N Number](input []N, low, high int) ([]N, int) { 10 | pivot := input[high] 11 | i := low 12 | for j := low; j < high; j++ { 13 | if input[j] < pivot { 14 | input[i], input[j] = input[j], input[i] 15 | i++ 16 | } 17 | } 18 | input[i], input[high] = input[high], input[i] 19 | return input, i 20 | } 21 | 22 | func QuickSort[N Number](input []N, low, high int) []N { 23 | if low < high { 24 | input, partition := Partition(input, low, high) 25 | input = QuickSort(input, low, partition-1) 26 | input = QuickSort(input, partition+1, high) 27 | } 28 | return input 29 | } 30 | 31 | func main() { 32 | listInt := []int32{3,5,1,2,6,7,4,2,} 33 | listFloat := []float32{3.3,.5,1.5,22.3,64.2,7.1,1.4,2,} 34 | sorted := QuickSort(listInt, 0, len(listInt)-1) 35 | fmt.Println(sorted) 36 | 37 | sortedFloats := QuickSort(listFloat, 0, len(listFloat)-1) 38 | fmt.Println(sortedFloats) 39 | 40 | } -------------------------------------------------------------------------------- /mergesort/mergesort.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Number interface { 6 | int | int8 | int16 | int32 | int64 | 7 | float32 | float64 8 | } 9 | 10 | func MergeSort[number Number](input []number) []number { 11 | if len(input) < 2 { 12 | return input 13 | } 14 | first := MergeSort(input[:len(input)/2]) 15 | second := MergeSort(input[len(input)/2:]) 16 | return Merge(first, second) 17 | } 18 | 19 | func Merge[number Number](a, b []number) []number { 20 | final := []number{} 21 | i := 0 22 | j := 0 23 | for i < len(a) && j < len(b) { 24 | if a[i] < b[j] { 25 | final = append(final, a[i]) 26 | i++ 27 | } else { 28 | final = append(final, b[j]) 29 | j++ 30 | } 31 | } 32 | 33 | for ; i < len(a); i++ { 34 | final = append(final, a[i]) 35 | } 36 | 37 | for ; j < len(b); j++ { 38 | final = append(final, b[j]) 39 | } 40 | return final 41 | } 42 | 43 | 44 | func main() { 45 | list := []int32{4,3,1,5,} 46 | list2 := []float64{4.3, 5.2, 10.5, 1.2, 3.2,} 47 | 48 | sorted := MergeSort(list) 49 | fmt.Println(sorted) 50 | 51 | sortedFloats := MergeSort(list2) 52 | fmt.Println(sortedFloats) 53 | } -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | Go Generic Algorithms 2 | ======================= 3 | 4 | Welcome friends! 👋 This repo is where I'll be attempting to capture some generic algorithms 5 | written in Go! 6 | 7 | Feel free to submit a PR and add any new algorithms if you fancy a challenge! 8 | 9 | ## BubbleSort Algorithm 10 | 11 | You can attempt to run the bubblesort algorithm example using the following commands: 12 | 13 | ```bash 14 | $ cd bubblesort 15 | $ go1.18beta1 run bubblesort.go 16 | Go Generics Tutorial 17 | [1 3 4 5] 18 | [1.2 3.2 4.3 5.2 10.5] 19 | ``` 20 | 21 | 22 | ## QuickSort Algorithm 23 | 24 | You can attempt to run the quicksort algorithm example using the following commands: 25 | 26 | ```bash 27 | $ cd quicksort 28 | $ go1.18beta1 run quicksort.go 29 | [1 2 2 3 4 5 6 7] 30 | [0.5 1.4 1.5 2 3.3 7.1 22.3 64.2] 31 | ``` 32 | 33 | ## Insertion Sort Algorithm 34 | 35 | You can attempt to run the insertion sort algorithm example using the following commands: 36 | 37 | ```bash 38 | $ cd insertionsort 39 | $ go1.18beta1 run insertionsort.go 40 | [1 3 4 5] 41 | [1.2 3.2 4.3 5.2 10.5] 42 | ``` 43 | 44 | ## Mergesort Algorithm 45 | 46 | You can attempt to run the mergesort algorithm example using the following commands: 47 | 48 | ```bash 49 | $ cd mergesort 50 | $ go1.18beta1 run mergesort.go 51 | [1 3 4 5] 52 | [1.2 3.2 4.3 5.2 10.5] 53 | ``` 54 | 55 | 56 | ## Selection Sort Algorithm 57 | 58 | You can attempt to run the quicksort algorithm example using the following commands: 59 | 60 | ```bash 61 | $ cd selectionsort 62 | $ go1.18beta1 run selectionsort.go 63 | [1 3 4 5] 64 | [1.2 3.2 4.3 5.2 10.5] 65 | ``` --------------------------------------------------------------------------------