diff --git a/02_selection_sort/Golang/01_selection_sort.go b/02_selection_sort/Golang/01_selection_sort.go new file mode 100644 index 0000000..118cc43 --- /dev/null +++ b/02_selection_sort/Golang/01_selection_sort.go @@ -0,0 +1,37 @@ +package main + +import "fmt" + +// Finds the smallest value in an array +func findSmallest(arr []int) int { + // Stores the smallest value + smallest := arr[0] + + // Stores the index of the smallest value + smallest_index := 0 + for i := 1; i < len(arr); i++ { + if arr[i] < smallest { + smallest = arr[i] + smallest_index = i + } + } + return smallest_index +} + +// Sort array +func selectionSort(arr []int) []int { + size := len(arr) + newArr := make([]int, size) + for i := 0; i < size; i++ { + // Finds the smallest element in the array and adds it to the new array + smallest := findSmallest(arr) + newArr[i] = arr[smallest] + arr = append(arr[:smallest], arr[smallest+1:]...) + } + return newArr +} + +func main() { + s := []int{5, 3, 6, 2, 10} + fmt.Println(selectionSort(s)) +} diff --git a/09_dynamic_programming/golang/01_longest_common_subsequence.go b/09_dynamic_programming/golang/01_longest_common_subsequence.go new file mode 100644 index 0000000..c8a8c67 --- /dev/null +++ b/09_dynamic_programming/golang/01_longest_common_subsequence.go @@ -0,0 +1,10 @@ +package main +import "fmt" + +func main(){ + if word_a[i] == word_b[j] { + cell[i][j] = cell[i-1][j-1]+1 + }else{ + cell[i][j] = max(cell[i-1][j], cell[i][j-1]) + } +}