
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Implement Linear Search Algorithm
In programming, to search for anything from an array, linked List, or from any other data structures we have a few search algorithms, one of which is Linear search. In the linear search, we iterate over the data structure from starting and search for the element till the last index.
The advantage of a linear search algorithm is that we can perform this search of both sorted and unsorted data. The disadvantage is that for sorted or unsorted both kinds of data it will take the same amount of time to find an element.
For example, we have an array of elements {10, 5, 3, 7, 6, 12} and we have to find element 3 then in the linear search algorithm we will
first, compare with 10 at index 0, and move to the next index as values are not equal
Now, we 5 will get compared to 3 and move to the next index as both are not equal
Now we have found the element at index 2
Algorithm
Step 1: Import the required packages at the top using the import keyword.
Step 2: Then the main function will get run first.
First, we are declaring and initialize the array.
Call the linearSearch() function to search the element by passing the array, size, and element to search in the parameter.
In the end, print the result whether the element is present or not.
Step 3.1: in the linearSearch() function
In the first method, we are running a for loop over the array.
Inside the loop, we are comparing the element with the value at the current index. If the value matches then we are returning the index.
In the end, we are returning ?1 if the element is not present.
Step 3.2: in the linearSearch() function
In the second method, the function is recursive.
The base condition is that the current index should be less than the size of the array else returning ?1.
If the value at the current index is equal to the value we are looking for then return the index.
Example 1
In this example, we are going to implement linear search by running a for loop from the 0 indexes to the last index of the array. On each index, we are comparing the current index element with the element we are searching and if both elements match then we return the index.
package main import "fmt" // function to print the array with array and // size of the array as argument func printArray(array []int, size int) { for i := 0; i < size; i++ { fmt.Print(array[i], " ") } fmt.Println() } // linear function to find an element in the array func linearSearch(array []int, size int, toFind int) int { // running for loop over the array for i := 0; i < size; i++ { //Comparing the current index value with the // value we want to find if array[i] == toFind { return i } } // returning -1 if value not present in the array return -1 } func main() { // declaring and initializing the // array using the shorthand method array := []int{10, 5, 3, 7, 6, 12} // declaring and initializing the // variable using the var keyword var toSearch int toSearch = 3 fmt.Println("Golang program to find an element in an array using linear search.") fmt.Print("array:") printArray(array, 6) // linear search function passing array and // variable as a parameter index := linearSearch(array, 6, toSearch) if index == -1 { fmt.Println(toSearch, "is not present in the array.") } else { fmt.Println(toSearch, "is present at index 2 in the array.") } }
Output
Element is present.
Golang program to find an element in an array using linear search. array: 10 5 3 7 6 12 3 is present at index 2 in the array.
Example 2
In this example, we are going to implement linear search using a recursive approach. There will be recursive calls in which we will increase the index on each function call. On each function call, we are comparing the current index element with the element we are searching and if both elements match then we return the index.
package main import "fmt" // function to print the array with array and // size of the array as argument func printArray(array []int, size int) { for i := 0; i < size; i++ { fmt.Print(array[i], " ") } fmt.Println() } // linear function to find an element in the array func linearSearch(array []int, currIndex int, size int, toFind int) int { if currIndex >= size { // returning -1 if value not present in the array return -1 } //Comparing the current index value with the // value we want to find if array[currIndex] == toFind { return currIndex } // calling linearSearch function for the next index return linearSearch(array, currIndex+1, size, toFind) } func main() { // declaring and initializing the // array using the shorthand method array := []int{10, 5, 3, 7, 6, 12} // declaring and initializing the // variable using the var keyword var toSearch int toSearch = 3 fmt.Println("Golang program to find an element in an array using linear search recursively.") fmt.Print("array:") printArray(array, 6) // linear search function passing array and // variable as a parameter index := linearSearch(array, 0, 6, toSearch) if index == -1 { fmt.Println(toSearch, "is not present in the array.") } else { fmt.Println(toSearch, "is present at index 2 in the array.") } }
Output
Golang program to find an element in an array using linear search recursively. array: 10 5 3 7 6 12 3 is present at index 2 in the array.
Conclusion
These are the two ways to perform a linear search in Golang. The second method has multiple function calls and for the array with a large number of elements, the stack will get overloaded. Doing this for a simple operation is not an appropriate programming way so the method one of running for loop will be a more efficient way to achieve this. To learn more about Golang you can explore these tutorials.