From 4c03f5702c95db2e2a45a6d2f9419314b778353c Mon Sep 17 00:00:00 2001 From: Sachinsharmak <89272sachin1@gmail.com> Date: Mon, 2 Oct 2023 11:54:00 +0530 Subject: [PATCH] Linear_Search --- algorithms/go/linear_search.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 algorithms/go/linear_search.go diff --git a/algorithms/go/linear_search.go b/algorithms/go/linear_search.go new file mode 100644 index 0000000..734b32a --- /dev/null +++ b/algorithms/go/linear_search.go @@ -0,0 +1,29 @@ +// Linear Search +// Time Complexity: O(n) +/* +Here in this code we first define a function called +linearSort which takes two arguments, an array and a search value. +Then we loop through the array and check +if the search value is equal to the current value in the array. +If it is, we return the index of that value. +If we don’t find the value, we return -1. +*/ +package main + +import "fmt" + +func linearSort(arr []int, s int) int { + for index, value := range arr { + if s == value { + return index + } + } + + return -1 +} + +func main() { + var n = []int{9, 1, 33, 21, 78, 42, 4} + + fmt.Println(linearSort(n, 78)) +}