Selection_Sort Golang (#35)
* recursion_Golang * go_fmt * selection_sort_Golang * rm-golang * selection_sort_Golang * selection_sort_Golang * selection_sort_Golang * dynamic_golang
This commit is contained in:
committed by
Aditya Bhargava
parent
d0ac45bcde
commit
1ab56fce62
37
02_selection_sort/Golang/01_selection_sort.go
Normal file
37
02_selection_sort/Golang/01_selection_sort.go
Normal file
@@ -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))
|
||||||
|
}
|
||||||
@@ -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])
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user