ALGORITHM OR METHOD
vs. DATA STRUCTURE
Problem: Compute the average of three numbers.
aver = (x + y + z)/3.
aver = (x/3) + (y/3) + (z/3).
Method (1) superior to Method (2); two less div-operations.
They access data in the same order: 〈x, y, z, aver〉.
Any improvement due to data-structure applies equally well to
Three variables x, y, z.
An array nums[0..2].
− This is inferior to (a) because accessing an array-item takes
more time than accessing a simple variable. (To access
nums[i], the executable code has to compute its address
addr(nums[i]) = addr(nums) + i*sizeof(int), which
involves 1 addition and 1 multiplication.)
− When there are large number of data-items, naming individual data-items is not practical.
− Use of individually named data-items is not suitable when a
varying number of data-items are involved (in particular, if
they are used as parameters to a function).
A Poor Implementation of (1): Using 3 additions and 1 division.
a = x + y; //uses 2 additional assignments
b = a + z;
aver = b/3;