Lecture 2
Lecture 2
Definition :
• A finite, clearly specified sequence of
instructions to be followed to solve a problem.
11.2
Properties of an algorithm
• Effectiveness
simple
can be carried out by pen and paper
• Definiteness
clear
meaning is unique
11.3
Properties of an algorithm
• Correctness
give the right answer for all possible cases
Finiteness
stop in reasonable time
11.4
ARRAYS
Solution
❑ In the first case, we need 100 instructions to read, 100
instructions to write and 100 instructions to process. The
total is 300 instructions.
❑ In the second case, we have three loops. In each loop we have
two instructions, for a total of six instructions. However, we
also need three instructions for initializing the index and three
instructions to check the value of the index. In total, we have
twelve instructions.
Example
11.11
Representation of Linear Array in Memory
• To calculate the address of any element of LA the formula
is:
Loc(LA[K]) = Base [LA] + w(K – Lower bound)
where
• Loc(LA[K]) = address of the element LA[K] of the array LA
• Base[LA] is the first element of the array
• W is the number of memory cell each element occupied
or the word size
• Lower bound is the index of the first element of the array
• K is the address of the array element
Example
• Suppose an array ST is used to store the no. of
students enrolled in BUK since 1984 to 2017 .
The array ST is picture below:
Find
a) Base (ST) b) Word size(w) c) Loc[1995]
d) Loc[2016] e) The lower bound of ST
f) The upper bound of ST
Traversing
• let A be a collection of data elements stored in
the memory of the computer
• Suppose we want to either print the contents of
each element of A or to count the number of
elements of A with a given property.
• This can be accomplished by traversing A,
that is, by accessing and processing
(frequently called visiting) each element of A
exactly once.
This algorithm traverses LA applying an operation PROCESS to each element of LA.
Inserting Algorithm
The above algorithm deletes the Kth element from a linear array LA and assigns it
to a variable ITEM i.e. DELETE{LA.N,K,ITEM).
Multi-dimensional
arrays
The arrays discussed so far are known as one-dimensional arrays
because the data is organized linearly in only one direction. Many
applications require that data be stored in more than one dimension.
Figure below shows a table, which is commonly called a two-
dimensional array. In fact, some programming languages allow the
number of dimensions for an array to be high as 7.
A two-dimensional array
Multi-dimensional arrays
• Ordinary arrays are indexed by a single integer. Also
useful, particularly in numerical and graphics
applications, is the concept of a multi-dimensional
array, in which we index into the array using an
ordered list of integers, such as in a[3,1,5].
• In practice, the dimensionality of an array rarely
exceeds three.
11.26
Two dimensional array
• Two-dimensional arrays are called matrices in mathematics and
tables in business applications;
• Hence , two-dimensional arrays are called matrix arrays.
• It is most common to index this array using the RC-convention, where
elements are referred in row, column fashion
• Let A be a two–dimensional m x n array.
• The first dimension of A contains the index set 1,......., m. with lower
bound 1 and upper bound m.
• The second dimension of A contains the index set 1,2,..... n, with
lower bound 1 and upper bound n.
• The pair of lengths m x n (read “m by n”) is called the size of the
array.
11.27
Representation of two dimentional array in memeory
The indexes in a one-dimensional array directly define the relative
positions of the elements in actual memory.
Multi-dimensional arrays can be stored as linear arrays in order to
reduce the computation time and memory.
11.30
Raw major order formula
11.31
Example 11.4
.
If the first element occupies the location 1000, the target element
occupies the location 1018.
Operations on array
11.35