1def LinearSearch(array, n, k):
2
3 for j in range(0, n):
4
5 if (array[j] == k):
6
7 return j
8
9 return -1
10
11
12array = [1, 3, 5, 7, 9]
13
14k = 7
15n = len(array)
16
17result = LinearSearch(array, n, k)
18
19if(result == -1):
20
21 print("Element not found")
22
23else:
24
25 print("Element found at index: ", result)
26
1//Java implementation of Linear Search
2
3import java.util.Scanner;
4
5public class LinearSearch {
6
7 public static void main(String[] args) {
8 // TODO Auto-generated method stub
9 Scanner sc = new Scanner(System.in);
10 int[] a = {10,20,50,30,40};
11 int key=sc.nextInt(),flag=0;
12
13 for(int i=0;i<a.length;i++)
14 {
15 if(a[i]==key)
16 {
17 flag=1;
18 break;
19 }
20 else
21 {
22 flag=0;
23 }
24 }
25 if(flag==1)
26 {
27 System.out.println("Success! Key ("+ key + ") found");
28 }
29 else
30 {
31 System.out.println("Error! This key (" + key + ") does not exist in the array");
32 }
33 }
34
35}
36
1#include <bits/stdc++.h>
2
3using namespace std;
4
5int search(int arr[], int n, int key)
6{
7 int i;
8 for (i = 0; i < n; i++)
9 if (arr[i] == key)
10 return i;
11 return -1;
12}
13
14int main()
15{
16 int arr[] = { 99,4,3,8,1 };
17 int key = 8;
18 int n = sizeof(arr) / sizeof(arr[0]);
19
20 int result = search(arr, n, key);
21 (result == -1)
22 ? cout << "Element is not present in array"
23 : cout << "Element is present at index " << result;
24
25 return 0;
26}
1def global_linear_search(target, array)
2 counter = 0
3 results = []
4
5 while counter < array.length
6 if array[counter] == target
7 results << counter
8 counter += 1
9 else
10 counter += 1
11 end
12 end
13
14 if results.empty?
15 return nil
16 else
17 return results
18 end
19end
1A linear search is the simplest method of searching a data set. Starting at the beginning of the data set, each item of data is examined until a match is made. Once the item is found, the search ends.