An Example of an Algorithm

Suppose you are given an unsorted list of numbers and want to find the largest number in this list.

Here's an informally described algorithm:

  1. When you begin, the first number is the largest number in the list you've seen so far.
  2. Look at the next number, and compare it with the largest number you've seen so far.
  3. If this next number is larger, then make that the new largest number you've seen so far.
  4. Repeat steps 2 and 3 until you have gone through the whole list.
And here is a more formal coding of the algorithm in a pseudocode that is similar to most programming language
Given: a list of numbers "list" 

largest = list[1]
counter = 2
while counter <= length(list)
    if list[counter] > largest
        largest = list[counter]
    counter = counter + 1
print largest

Are their other algorithms for solving this general problem?

It's easy to prove that:

last modified on