Implementation (algorithm specification)Examples of linear search

Algorithms are created to allow users to tell a computer how to solve a problem. Understanding how to construct five of the most common algorithms is a critical skill for budding software developers.

Part ofComputing ScienceRevise: Software design and development

Examples of linear search

Visual Studio 2010 (Similar to VB5, VB6 and Subsequent Visual Basic.NET languages)

In this example it is presumed that the array/list has already been populated.

In this example a label is used to output the result. It is possible to use other objects for output.

item_found = false
counter = 0
desired_item = inputbox (“Please enter the item that you would like to search for”, “Search Value”)
Do
If desired_item = nation(counter) Then
item_found = true
labelresult.Text = "The program found " & desired_item & " at position " & counter & " within the nation array."
Else
counter = counter + 1
End If
Loop until counter = 10 Or item_found = true
If item_found = false Then
labelresult.Text = "The program could not find " & desired_item & " within the nation array"
End If

LiveCode

Remember - Live code will index from 1 rather than 0!

In this example it is presumed that the array/list has already been populated.

put false into itemfound
put 1 into counter
ask "Please enter the item that you would like to search for"
put it into desireditem
repeat until counter = 11 or itemfound = true
if desireditem = nation[counter] then
put true into itemfound
put “The program found " && desired_item && ”at position” && counter && “within the nation array.” into field "output"
else
put counter + 1 into counter
end If
end repeat
if itemfound = false then
put “The program could not find " && desired_item “within the nation array.” into field "output"
end if

Python – Version 3.x

In this example it is presumed that the array/list has already been populated.

item_found = False
counter = 0
desired_item = str(input(“Please enter the item that you would like to search for”))
for counter in range (10):
while counter ˂ 10 and not (item_found):
If nation_list[counter] == desired_item:
item_found = True
print ("The program found ”, desired_item, “ at position ”, counter, “ within the nation array.”)
if item_found = False:
print (“The program could not find ”, desired_item, “ within the nation array”.)

Java

In this example it is presumed that the array/list has already been populated.

boolean found = false;
int counter = 0;
desired_item= JOptionPane.showInputDialog("Please enter the item that you would like to search for");
do
{
if(nation[counter]=desired_item)
{
Found = true;
System.out.println("The program found " + desired_item + “at position ” + desired_item + “ of the nation array”);
{
counter ++
} while (counter˂10 or found = false);
if(found=false)
{
System.out.println("The program did not find " + desired_item + “ within the nation array.”);
}

True BASIC

Remember – True Basic will index from 1 rather than 0!

In this example it is presumed that the array/list has already been populated.

LET found = 0
LET counter = 1
INPUT PROMPT “Please enter the item that you would like to search for”: desired_item
DO
IF desired_item = nation(counter) THEN
LET found = 1
PRINT "The program found ”; desired_item; “ at position”; counter; “of the nation array."
ELSE
counter = counter + 1
END IF
LOOP UNTIL counter = 11 OR found = 1
IF found = 0 THEN
PRINT "The program did not find ”; desired_item; “ within the nation array”
END IF

Xojo (Formerly REALbasic)

In this example it is presumed that the array/list has already been populated.

The example presumes that you have access to a pre-designed method for creating an InputBox.

A static text box called ‘output’ has been used to output the minimum value.

item_found = false
counter = 0
desired_item = val(InputBox("Please enter the item that you would like to search for")
Do
If nation(counter)=desired_item Then
item_found = true
output.text = “The program found ” + desired_item + “ at position ” + counter + “ of the nation array.”
Else
counter = counter + 1
End If
Loop Until item_found = true or counter = 10
If item_found = false Then
output.text = “The program did not find ” + desired_item + “ within the nation array.”
End

Pascal

In this example it is presumed that the array/list has already been populated.

begin
found:=false;
counter:=0;
Writeln (‘Please enter the item that you would like to search for’)
Readln(desired_item);
repeat
if (nation[counter] = desired_item) then found:=true;
writeln(output, 'The program found ’, desired_item, ‘ at position ’, counter, ‘ of the nation array.’);
else
counter:=counter+1;
until (found = true) or (counter = 10) ;
if found:=false then
writeln(output, 'The program did not find ’, desired_item, ‘ in the nation array.’);
end;

Comal

In this example it is presumed that the array/list has already been populated.

found := FALSE
counter% := 0
PRINT "Please enter a score between 0 and 50";
INPUT desired_item
REPEAT
IF nation (counter%) = desired_item THEN found := TRUE
PRINT “The program found ”, desired_item%, “ at position ”, counter%, “of the nation array.”
ELSE
counter% := counter% + 1
END IF
UNTIL (found = TRUE) OR (counter% = 10)
IF found:= FALSE THEN
PRINT “The program did not find ”, desired_item%, “ within the nation array.”
END IF