Sie sind auf Seite 1von 22

Linear Data Structure:Arrays

Tazeen Muzammil
MSSE
Physical Memory

• We have to store our data structures in the


memory of our computer.
• Memory is organized into banks, rows,
columns etc.
• We supply a bank number, row number etc (=
an address), memory returns us the contents

• Address → contents
Physical Memory
Arrays
• The array is the most commonly used data storage
structure; its built into most programming languages.
• Since arrays are usually easy to traverse, search and sort,
they are frequently used to store relatively permanent
collection of data.
• But if the size of the structure and the data in the structure
is constantly changing the array may not be useful.
• The elements of the array may be denoted by the bracket
notation
A[1], A[2], A[3]…….A[n]
Linear Array
• A linear array is a list of finite number n of
homogeneous data elements such that:
– The elements of the array are referenced
respectively by an index set consisting of n
consecutive numbers.
– The elements of the array are sorted respectively
in successive memory location.
The number n of elements is called the length or
size of the array.
Length of the Array
• The length or the number of data elements of
the array can be obtained by this formula:

Length= UB-LB+1
where:
UB is the largest index, called the upper bound
LB is the smallest index, called the lower bound
Representation of Linear Arrays in Memory

• Let LA be a linear array


• The elements of LA are stored in successive memory cells.
So the computer does not need to keep track of the
address of every element of LA, but needs to keep track
only of the address of the first element of LA denoted by:
Base(LA)

• Using this address the computer can calculate the address


of any element of LA by the following formula:
LOC(LA[k])=Base(LA)+w(K-LB)
where:
K is the array index
W is the width of the record
Representation of Linear Arrays in Memory
Exercise
• Consider the linear arrays AAA(5:50), BBB(-
5:10), and CCC(18)
a) Find the number o elements in each array
b) Suppose Base(AAA)=300 and w=4 for AAA. Find
the address of AAA[15], AAA[35] and AAA[55]
Traversing Linear Array
• Let A be collection of data elements stored in the
memory of the computer.
• If we want to count the number of elements of A
or we want to print the contents of each element
of A, this can be do by traversing A.
• Traversing means accessing and processing each
element exactly once.
• Traversing a linear data structure is easier as
compared to traversing a non linear structure.
Algorithm (using repeat-while loop)
• Here LA is a linear array with lower bound LB and
upper bound UB. This algorithm traverses LA
applying an operation PROCESS to each element of
LA.
1. [Initialize counter] Set K=LB
2. Repeat steps 3 and 4while K< UB
3. [Visit element]Apply PROCESS to LA[K]
4. [Increase counter] Set K=K+1
5. [End of step 2 loop]
6. Exit.
Algorithm (using repeat-for loop)
• Here LA is a linear array with lower bound LB
and upper bound UB. This algorithm traverses
LA applying an operation PROCESS to each
element of LA.
1. Repeat for K=LB to UB
2. [Visit element]Apply PROCESS to LA[K]
3. [End of loop]
4. Exit.
Exercise
• Consider the array AUTO which records the
number of automobiles sold each year from
1932 through 1984
a) Find the number NUM of years during which
more than 300 automobiles were sold.
b) Print each year and the number of
automobiles sold in that year.
Inserting and Deleting
• Let A be collection of data elements stored in the
memory of the computer.
• Inserting refers to the operation of adding
another element to the collection A. and
• Deleting refers to the operation of removing one
of the element from A.
• Inserting an element at the end of a linear array
can be easily done provided the memory space
allocated for the array is large enough.
Inserting Elements
Algorithm for Insertion
• LA is the linear array with N elements and K is the positive integer
such that K< N. This algorithm inserts an element ITEM into the Kth
position in LA.

1. [Initialize counter] Set J=N


2. Repeat steps 3 and 4while J > K
3. [Move Jth element down]Set LA[J+1] = LA[J]
4. [Decrease counter]Set J=J-1
5. [End of step 2 loop]
6. [Insert element]Set LA[K]=ITEM
7. [Reset N]Set N=N+1
8. Exit.
Algorithm for Deletion
• LA is the linear array with N elements and K is the positive
integer such that K< N. This algorithm deletes an element
ITEM into the Kth position in LA.

1. Set ITEM =LA[K]


2. Repeat for J = K to N-1
3. [Move J+1 element upward]Set LA[J] = LA[J+1]
4. [End of loop]
5. [Reset N]Set N=N-1
6. Exit.
Benefits of Using Array
• We can retrieve an array element from its
index in constant time, meaning it costs us
asymptotically nothing to look up a record.
• Consist solely of data, no space wasted on
links.
• Physical continuity/memory locality: if we look
up element i, there is a high probability we
will look up element i+1 next
Drawbacks of Using Array
• Inflexible: we have to decide in advance how much
space we want when the array is allocated. Once the
block of memory for the array has been allocated,
that’s it –we’re stuck with the size we’ve got.
• If we try to write past the end of the array (overflow),
we’ll be intruding on memory allocated for
something else causing a segmentation fault.
• We can compensate by always allocating arrays
larger than we think we’ll need, but this wastes a lot
of space.
Multi dimensional Arrays
• A two-dimensional m*n array A is a collection of
m.n data elements such that each element is
specified by a pair if integers(such as J,K), called
subscript.
• Two-dimensional arrays are sometime called
matrix array.
• Two-dimensional 3*4 array A
1. A[1,1] A[1,2] A[1,3]
2. A[2,1] A[2,2] A[2,3]
3. A[3,1] A[3,2] A[3,3]
Representation of Two-Dimensional Arrays in Memory
A is a two dimensional 3*4
array
Column-major order Row-major order
[1,1] [1,1]
[2,1] [1,2]
C1 r1
[3,1] [1,3]
[1,2] [1,4]
[2,2] [2,1]
C2
[3,2] [2,2]
r2
[1,3] [2,3]
[2,3] C3 [2,4]
[3,3] [3,1]
[1,4] [3,2]
r3
[2,4] C4 [3,3]
[3,4] [3,4]
Matrix Multiplication
• Let A be an M* P matrix, and let B be a P*N matrix
array. This algorithm stores the product of A and B in
a M*N matrix array C
1. Repeat Steps 2 to 4 for I=1to M
2. Repeat Steps 3 to 4 for J=1to N
3. Set C[I,J]=0 Initialize C[I,J]
4. Repeat for K=1 to P
5. C[I,J]=C[I,J]+A[I,K]*B[K,J]
[END of inner loop]
[END of Step 2 loop]
[END of Step 1 loop]

Das könnte Ihnen auch gefallen