0% found this document useful (0 votes)
97 views35 pages

Lecture 2

An algorithm is defined as a finite sequence of unambiguous instructions to solve a problem. Key properties of algorithms include effectiveness, definiteness, correctness, and finiteness. Arrays allow storing and processing a collection of data elements more efficiently than individual variables. Arrays use indexes to refer to elements, allowing loops to process all elements with fewer instructions than processing each one individually. Multi-dimensional arrays generalize this to multiple indexes, commonly two indexes for two-dimensional arrays representing matrices or tables. Arrays are stored linearly in memory, with computations to translate between indexes and memory addresses. Common array operations include searching, insertion, deletion, retrieval and traversal. Arrays are best suited when many search/retrieval operations are needed and few

Uploaded by

Basiru Ibrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views35 pages

Lecture 2

An algorithm is defined as a finite sequence of unambiguous instructions to solve a problem. Key properties of algorithms include effectiveness, definiteness, correctness, and finiteness. Arrays allow storing and processing a collection of data elements more efficiently than individual variables. Arrays use indexes to refer to elements, allowing loops to process all elements with fewer instructions than processing each one individually. Multi-dimensional arrays generalize this to multiple indexes, commonly two indexes for two-dimensional arrays representing matrices or tables. Arrays are stored linearly in memory, with computations to translate between indexes and memory addresses. Common array operations include searching, insertion, deletion, retrieval and traversal. Arrays are best suited when many search/retrieval operations are needed and few

Uploaded by

Basiru Ibrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

What is An Algorithm ?

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

Imagine that we have 100 scores. We need to read them,


process them and print them. We must also keep these
100 scores in memory for the duration of the program.
We can define a hundred variables, each with a different
name, as shown in Figure below

Figure 1.1 A hundred individual variables 11.5


But having 100 different names creates other problems. We
need 100 references to read them, 100 references to process
them and 100 references to write them. Figure below shows
a diagram that illustrates this problem.

Fig. 1.2 Processing individual variables


An array is a sequenced collection of elements, normally of
the same data type, although some programming languages
accept arrays in which elements are of different types.

We can refer to the elements in the array as the first element,


the second element and so forth, until we get to the last
element.

Fig. 1.3 Arrays with indexes


We can use loops to read and write the elements in an array.
We can also use loops to process elements. Now it does not
matter if there are 100, 1000 or 10,000 elements to be
processed—loops make it easy to handle them all. We can
use an integer variable to control the loop and remain in the
loop as long as the value of this variable is less than the total
number of elements in the array .

We have used indexes that start from 1;


some modern languages such as C,
C++ and Java start indexes from 0.
Fig. 1.4 Processing an array
Example
Compare the number of instructions needed to handle 100
individual elements in Figure 1.3 and the array with 100 in
Figure 1.4. Assume that processing each score needs only one
instruction.

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

The number of cycles (fetch, decode, and execute phases) the


computer needs to perform is not reduced if we use an array. The
number of cycles is actually increased, because we have the extra
overhead of initializing, incrementing and testing the value of the
index. But our concern is not the number of cycles: it is the
number of lines we need to write the program.

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.

The Figure below shows a two-dimensional array and how it is stored


in memory using row-major or column-major storage. Row-major
storage is more common.
.

Memory layout of arrays


• For a linear array LA, the computer does not keep track of the
address LOC(LA[K]) of every element LA[K] of LA, but does keep
track of Base(LA), the address of the first element of LA.
• The computer uses the formula below to find the address of LA[K] .

LOC(LA[K]) = Base(LA) + w (K-1)

• w is the number of words per memory cell for the


array LA, and 1 is the lower bound of the index set of LA.
• A similar situation also holds for any two-dimensional m x n array A.
• That is, the computer keeps track of Base(A) – the address of the
first element A[1,1] of A – and computes the address LOC(A[J, K]) of
A[J, K].
column major order formula

LOC(A[J, K]) = Base(A) + w[M(K-1) + (J-1)]

11.30
Raw major order formula

LOC(A[J, K]) = Base(A) + w[N(J-1) + (K-1)]

11.31
Example 11.4

We have stored the two-dimensional array students in memory.


The array is 100 × 4 (100 rows and 4 columns). Show the address
of the element students[5][3] assuming that the element
student[1][1] is stored in the memory location with address 1000
and each element occupies only one memory location. The
computer uses row-major storage.
Solution

.
If the first element occupies the location 1000, the target element
occupies the location 1018.
Operations on array

Although we can apply conventional operations defined for


each element of an array , there are some operations that we
can define on an array as a data structure. The common
operations on arrays as structures are searching, insertion,
deletion, retrieval and traversal.

Although searching, retrieval and traversal of an array is an


easy job, insertion and deletion is time consuming. The
elements need to be shifted down before insertion and
shifted up after deletion.
Application
Thinking about the operations discussed in the previous
section gives a clue to the application of arrays. If we have a
list in which a lot of insertions and deletions are expected
after the original list has been created, we should not use an
array. An array is more suitable when the number of
deletions and insertions is small, but a lot of searching and
retrieval activities are expected.
THANK YOU

11.35

You might also like