linear search

Solutions on MaxInterview for linear search by the best coders in the world

showing results for - "linear search"
Debora
28 Jan 2016
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
Marie
23 Apr 2018
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
Jana
12 Oct 2016
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}
Lennart
22 Sep 2016
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
Arianna
27 Nov 2020
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.
queries leading to this page
how does linear search work 3fhow to linear searchliear searchwhat does linear search dotechnique used by linear searchlinear search graphlinear search and binary search implementation and nanlysiswhere is linear searching used 3f 2alinear search meaningalgorithm of linear searchwhat is linear search explain with examplec linear search 22how does linear search work 3f 22linear search definitionhow does a linear search worklinear search recursivelinear search on array linear search algithmlinear search in arraydoes linear search always worklinear search functionlinear search examplewhen to use linear search algorithmhow to linear search algorithmwhat is a linear searchlinear search method in chow to do a linear searchexplain linear searchlinear search in dbms examplewhat is linear search filelinear search algorithm in clinear search explanationalgorithm to search an element using linear searchlinear search algorithm explainedwhat we to do linear searchlinear search isprogramming linear search definitionlinear search iimplement linear search algorithmdoes a linear search algorithm start at the end or beginningis linear search algorithmwhat linear search algorithmsearch algorithmwhy linar search is linearexample of linear search in data structurelinear search explainedsequential search in clinear search is also called aspseudo code linear searchlinear search c 2b 2b programsequential searchhow to do a lineear searchlinear search search for an element in an array 2c we can use the algorithmswhere is linear searching used 3flinear search examplesexample of linear searchlinear finding algorithm with whilelearner searching in cliner searchthe linear search algorithmlinear search code linear search algorithm awhat is the used programming technique is used in linear searching algorithminear searchalgorithm for linear searchdefinition of linear search algorithmlinear search why we use linear searchhow does linear search workwhat is linear searchwhere is linear search usedwhat is linear search in c 2b 2blinear search definition in cwhere is linear searching usedlinear search algorithmexample of linear searchwhat is linear search algorithmlinear search pseudocode c 2b 2blinear search onlinechar sort using lenear searchlinear search algorithm syntaxsequenital searchwhen to use linear searchlinear search problemlinear searchsearch algorithm linearlinear search algorithm techniquewhat linear search linear searchlinear search is also known as explain linear search algorithmhow linear search workslinear search arraywrite a program to search an element in the array using linear search determine the time required to search the element linear search methodlinear search odiscuss about linear searchlinear seatch algorithmwhere to use linear searchwrite a algorithm for linear search method for searching the elements in the arraylinear search functinlinear search exmplesequqntia l searchlinear search ggkey element in linear searchlinear search in cwhat is in linear searchlinear database searchlinear search in programminglinear search programlinear search function c 2b 2blinear search algorithm examplewhy linear search is usedlinear search 5clinear search algorithm 3awhen is linear search usedlinear search