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.
Linear search in Go
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
.
What is linear search?
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
How linear search works: A step-by-step example
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:
Linear search algorithm in Go
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)}}
Step-by-step code explanation
Line 1: We declare the
mainpackage. In Go, themainpackage is used to define the entry point for executable programs.Line 3: The
fmtpackage 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 valuetargetwe're searching for in the array.The function returns an
int, which is the index of the target element if found, or-1if the element is not in the array.
Line 9: The
forloop iterates through each element in the array usingrange. Therangekeyword returns two values: the indexiand the value of the elementnumat that index.Lines 10–21: If
nummatches thetarget, the function immediately returns the indexiwhere the target was found.Line 14: If no match is found after iterating through the entire array, the function returns
-1to indicate the element is not present.Lines 17–31: The
mainfunction is defined to call thelinearSearchfunction 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.
Time complexity of linear search algorithm
The time complexity of the linear search algorithm is
Space complexity of linear search algorithm
The space complexity of linear search is
Frequently asked questions
Haven’t found what you were looking for? Contact Us
What is another name for the linear search algorithm?
What search algorithm is more efficient than linear search?
What is the fastest search algorithm?
What are the disadvantages of linear search?
What is a real-life example of a linear search?
Free Resources