If they are equal return the position 1 if not try a2

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: e algorithm in pseudocode: procedure max (a1, a2, …., an: integers) max := a1 for i := 2 to n if max < ai then max := ai return max —༉  This algorithm returns the largest element in the set. Some Example Algorithm Problems —༉  Three classes of problems will be studied in this section. 1. Searching Problems: finding the position of a particular element in a list. 2.  Sorting problems: putting the elements of a list into an increasing order. 3.  Optimization Problems: determining the optimal value of a particular quantity over all possible inputs. —༉  Optimal value means maximum or minimum Linear Search Algorithm —༉  It locates an item in a list by examining elements in the list one at a time: —༉  First compare x with a1. If they are equal, return the position 1. —༉  If not, try a2. If x = a2, return the position 2. —༉  Keep going, if no match is found when the entire list is scanned, return 0. procedure linear search (x: integer, a1, a2, …,an: distinct integers) i := 1 whi...
View Full Document

This document was uploaded on 02/27/2014 for the course CS 215 at SIU Carbondale.

Ask a homework question - tutors are online