Standard Algorithms

Linear Search

A linear search is a search through a list, comparing each item with the search criteria. It returns the position of the item or displays a message to say it cannot be found.

Algorithm:

Get search_item

 

REPEAT for all items in list

IF current_item = search_item THEN

position = current_item_position

EXIT LOOP

END IF

END REPEAT

 

Display position

 

Count Occurrences 

Counts the amount of times an item appears in the list

Algorithm:

Get search_item

occurrences = 0

 

REPEAT for all items in the list

IF current_item = search_item THEN

occurrences = occurrences + 1

END IF

END REPEAT

 

Display occurrences

 

Finding the Maximum/Minimum

This finds the largest and the smallest item in the list

Algorithm:

maximum = o

 

REPEAT for all items in the list

IF current_item > maximum THEN

maximum = current_item

END IF

END REPEAT

 

Display maximum

 

 

minimum = 999

 

REPEAT for all items in the list

IF current_item < minimum THEN

minimum = current_item

END IF

END REPEAT

 

Display minimum

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s