Difference between revisions of "Linear Search"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
Line 1: Line 1:
{{Need_Expanding}}
+
 
 
Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.
 
Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.
  
Here's a simple example in Python 3.
+
Here's a simple example in C#:
 +
In C#:
 +
<syntaxhighlight lang="csharp" line>
 +
string toSearch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 +
char[] searchArray = toSearch.ToCharArray();
 +
bool found = false;
 +
int i = 0;
 +
 
 +
while(found == false && i != searchArray.Length){
 +
    if(searchArray[i] == 'W'){
 +
        found = true;
 +
      } else {
 +
        i++;
 +
      }
 +
}
 +
Console.WriteLine("Found W at position: " + i);
 +
</syntaxhighlight>
 +
 
 +
Here's a simple example in Python 3:
 
<syntaxhighlight lang="python" line>
 
<syntaxhighlight lang="python" line>
 
ar = [["A", 1], ["B", 2], ["C", 3], ["D", 4], ["E", 5]];
 
ar = [["A", 1], ["B", 2], ["C", 3], ["D", 4], ["E", 5]];
Line 21: Line 39:
 
##Looking for 'l' None
 
##Looking for 'l' None
  
</syntaxhighlight>
 
 
In C#:
 
<syntaxhighlight lang="csharp" line>
 
string toSearch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
char[] searchArray = toSearch.ToCharArray();
 
bool found = false;
 
int i = 0;
 
 
while(found == false && i != searchArray.Length){
 
    if(searchArray[i] == 'W'){
 
        found = true;
 
      } else {
 
        i++;
 
      }
 
}
 
Console.WriteLine("Found W at position: " + i);
 
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 21:20, 21 May 2017

Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.

Here's a simple example in C#: In C#:

 1 string toSearch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 2 char[] searchArray = toSearch.ToCharArray();
 3 bool found = false;
 4 int i = 0;
 5 
 6 while(found == false && i != searchArray.Length){
 7      if(searchArray[i] == 'W'){
 8         found = true;
 9       } else {
10         i++;
11       }
12 }
13 Console.WriteLine("Found W at position: " + i);

Here's a simple example in Python 3:

 1 ar = [["A", 1], ["B", 2], ["C", 3], ["D", 4], ["E", 5]];
 2 ##Array to be searched
 3 
 4 def linearSearch(arr, toFind):
 5     for x in arr: ##Goes by element 1 by 1
 6         if  x[0] == toFind: return x[1]; ##If you find the item, return the value.
 7     return None; #If you don't, return None for nothing.
 8 
 9 print ( "Looking for 'A'", linearSearch(ar, "A") );
10 print ( "Looking for 'E'", linearSearch(ar, "E") );
11 print ( "Looking for 'l'", linearSearch(ar, "l") );
12 
13 ##Return:
14 ##Looking for 'A' 1
15 ##Looking for 'E' 5
16 ##Looking for 'l' None