Linear search, also known as the sequential search algorithm, is one of the simplest search techniques. It is primarily used to locate an element within an unordered list by examining each element one by one.
Key takeaways:
Linear search is a simple algorithm used to find an element in an unordered list by checking each element one by one until a match is found or the list ends.
Implementing linear search in Go is straightforward. The code involves iterating over a slice and comparing each element to the target value until a match is found.
The time complexity of linear search is
, where is the number of elements in the list. The space complexity of linear search is
.
Linear search, also known as sequential search, is a simple algorithm used to find a specific element in an unordered list. It works by checking each element in the list one by one until the target element is found or the entire list has been searched. Due to its straightforward approach, linear search is easy to implement but can be inefficient for large datasets, as it has a time complexity of
The concept behind linear search is simple: it checks each element in a list one by one until it finds the target element or reaches the end of the list. Let's walk through an example to illustrate how this works.
Consider the list of 7 elements: [2, 67, 12, 10, 23, 4, 9]
. If we want to find the index of the number 4
in this list, linear search will start by comparing the first element (2
) with the target (4
). Since they don’t match, it moves to the next element (67
) and repeats the process.
The search continues, comparing 12
, 10
, 23
, and then 4
. When it finds 4
, the algorithm stops and returns the index where 4
is located—in this case, index 4
.
If the target element was not in the list, linear search would continue checking all elements until it has iterated through the entire list.
This is illustrated below:
Let’s walk through the process of implementing the linear search algorithm in Go.
package mainimport "fmt"// linearSearch performs a search for the target element in the given array.// It returns the index of the element if found, or -1 if not found.func linearSearch(arr []int, target int) int {// Loop through the array using range to check each elementfor i, num := range arr {if num == target {return i // Return the index if the target element is found}}return -1 // Return -1 if the target element is not found in the array}func main() {// Example array and target valuearr := []int{2, 67, 12, 10, 23, 4, 9}target := 4// Perform the search and get the index of the target elementindex := linearSearch(arr, target)// Output result based on whether the element was found or notif index != -1 {fmt.Printf("Element %d found at index %d\n", target, index)} else {fmt.Printf("Element %d not found in the list\n", target)}}
Line 1: We declare the main
package. In Go, the main
package is used to define the entry point for executable programs.
Line 3: The fmt
package is imported to allow formatted input and output operations. In this program, it's used to print messages to the console (such as the result of the search).
Line 7: We declare the function linearSearch
:
It takes two arguments: a slice (array) of integers, arr
, to search through, and the integer value target
we're searching for in the array.
The function returns an int
, which is the index of the target element if found, or -1
if the element is not in the array.
Line 9: The for
loop iterates through each element in the array using range
. The range
keyword returns two values: the index i
and the value of the element num
at that index.
Lines 10–21: If num
matches the target
, the function immediately returns the index i
where the target was found.
Line 14: If no match is found after iterating through the entire array, the function returns -1
to indicate the element is not present.
Lines 17–31: The main
function is defined to call the linearSearch
function and display the results.
If you're looking to deepen your understanding of Go, we recommend exploring the resources like An Introduction to Programming in Go and Golang for Programmers, which provide comprehensive insights into the language and its features.
The time complexity of the linear search algorithm is
The space complexity of linear search is
Haven’t found what you were looking for? Contact Us
Free Resources