GO Program to Implement Linear Search

For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:

Searching Algorithms is the most common process that you will see being implemented everywhere to check for an element or retrieve an element from any data structure where it is stored. 

In this Go program, we need to search an element from an array by using linear search. Linear search is a very common and popular searching algorithm. In this technique, each element is examined by iterating over the data collection & searching if the given element is present or not. If a match is found then that particular item is returned, otherwise, the search continues sequentially till the end of the data collection. That means a sequential search is made over all items one by one. Note that the time complexity of the Linear search algorithm is O(n).

 

HOW TO IMPLEMENT LINEAR SEARCH

Here we are explaining how to perform a linear search in the Go program. We can use the built-in function fmt.println() to print anything and fmt.scanln() for reading the values. This function is defined under the fmt package and it helps to write standard output. In order to use these functions, we need to import the “fmt” package.

Here variable holds the array elements. Other variables n, x is used as the size of the array and searching element respectively. Use for loop to read array elements and perform Linear search by calling function linearsearch(A, n, x). Use for loop in this function to move from the beginning of the list to the end. It will check each element by iterating through the loop as A[i] = x. If a match is found then print a particular array index and return that particular item, otherwise continue search sequentially till the end of the data collection. Finally, if there is no match with the search item then the print element is not found.

Given below are the steps which are used in the Go program to implement linear search. 

ALGORITHM:

STEP 1: Import the package fmt

STEP 2: Start function main()

STEP 3: Declare the variable n, x, i

STEP 4: Read the size of the array as n

STEP 5: Define the array A[] and

STEP 6: Read the A[] array elements by using a for loop.

STEP 7: Read the number to be searched as x.

STEP 8: Call the function linearSearch(A, size, x)

STEP 9: Exit

Steps To Implement Linearsearch(A, Size, X) Function

Step 1: Set i to 0

Step 2: if i > n then go to step 7

Step 3: if A[i] = x, then go to step 6

Step 4: Set i to i + 1

Step 5: Go to Step 2

Step 6: Print position of element found and go to step 8

Step 7: Print element not found

Step 8: Exit

 

GO Source Code

                                          package main
import "fmt"

func linearSearch(A []int, n int, x int) {
 for i := 0; i < n; i++ {
  if A[i] == x {
   fmt.Println("Number found at position : ", i+1)
   return
  }
 }

 fmt.Println("Number not found")
 return

}

func main() {
 fmt.Println("Enter the size of the array")
 var n int
 fmt.Scan(&n)

 var x int
 A := make([]int, n, 100)
fmt.Println("Enter elements of the array : ")

 for i := 0; i < n; i++ {
  fmt.Scan(&A[i])
 }

 fmt.Println("Enter the number to be searched")
 fmt.Scan(&x)

 linearSearch(A, n, x)

}

                                      

OUTPUT

Enter the size of the array
5
Enter elements of the array :
10
55
22
80
63
Enter the number to be searched
22
Number found at position : 3