CS 240: Algorithms and Data Structures
James Madison University, Fall 2021

For each of the algorithm listings below:

Listing 1

Count all arithmetic operations.

Listing 2

Count all arithmetic operations.

 

Listing 3

Count assignments to s.

  PROCEDURE DoStuff(numbers1, numbers2)
    s <- 0
    
    FOR x IN numbers1 DO
      FOR y IN numbers2 DO
         IF x < y DO
           RETURN 0
         ELSE
           s <- s + x
         ENDIF
       ENDFOR
    ENDFOR
    
    FOR x IN numbers2 DO
      s <- s + x
    ENDFOR
    
    RETURN s
  

Listing 4

Count all arithmetic operations.

 

Listing 5

Select an appropriate basic operation.

Listing 6

Use incrementing sum as the basic operation. For the sake of simplicity, you may assume that the length of numbers is a power of 2.

Listing 7