PWP wiki processor

Searching

| StartPage | WikiPages | AdditionalFiles |

Searching Data.

Searching Data.

There are two main methods for searching arrays and these are the Linear search and the Binary search.

Linear Search Algorithm

found = false
output "What number are you looking for you"
input x
for i = 1 to n
if Array[i] = x then 
  output "x found at location ";i
  found = true
end if
next i
if found = false then
  output "number not found."
endif

Binary Search Algorithm

low = 0;
high = length of array
found = false
output "What number are you looking for you"
input x
while (low <= high)
 middle = ( low  + high ) / 2;
 if( x == array[  middle ]) then
   Output "Item found at location "; middle
   Found = True
 else if( x < array[ middle ]) then
     high = middle - 1;      //search low end of array
   else
     low = middle + 1;		//search high end of array
   end if
 end if
loop
If (found = False) then
 Output "Item not found"
End if

   (Powered by PWP Version 1.4.2)