Find Largest Algorithm

 

  1. read n // the size of the list
  2. read A1, A2, ..., An
  3. set largest_so_far <- A1
  4. set location <- 1
  5. set i <- 2
  6. While i < n do
  7.     if Ai > largest_so_far then
  8.       set largest_so_far <- Ai
  9.       set location <- i // end if
  10.    i <- i + 1 // end while
  11. write largest_so_far, location
  12. stop

 

Counting the worst-case number of steps, as a function of n, we obtain

TFind_largest(n) =

(number of steps in loop) x (number of times through n) + (number of steps in linear part) =

5(n - 1) + 7 =

5n + 2

[Prev][TOC][Next]

CS1104 Main Page
Last Updated 01/05/2000
© L.Heath, 2000