You are on page 1of 22

Laecture 2:Linear Data

Structure:Arrays
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[0],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

Q1. Consider the linear arrays AAA(5:50), BBB(-5:10), and CCC(18)


a) Find the number of 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]
Q2. Consider the array AUTO, which records the number of
automobiles sold each year from 2000 through 2006. The
Base(AUTO)=200, and w=4 words per memory cell for AUTO.
c) Find the address of AUTO[2004].
Arrays in C#
• How to declare an array?
The general form of declaring a simple (one dimensional) array is.
type[] array-name;
In your C# program you can declare an array like
int[] Age;
Declaring them (as shown above) does not actually create the arrays. In
C#, arrays are objects and must be instantiated. The following examples
show how to create arrays:
• How toint[]
initialize an array?
Age = new int[5];

C# provides simple and straightforward ways to initialize arrays at


declaration time by enclosing the initial values in curly braces ({}).
int[] Age= new int[5] {20, 22, 33, 44, 26};
You can omit the size of the array, like this:
int[] Age = new int[] {20, 22, 33, 44, 26};
You can also omit the newoperator if an initializer is provided, like this:
int[] Age= {20, 22, 33, 44, 26};
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 done 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

• Here LA is a linear array with lower bound LB and upper bound UB.
This algorithm traverses LA applying a PROCESS to each element of
LA.

1. [Initialize counter ] St K=LB


2. Repeat Step 3 and 4 while K<
UB 1. Repeat for K= LB to UB
3. [Visit element] Apply 2. Apply PROCESS to LA[K}
PROCESS to LA[K} [End of loop]
4. [Increase counter] Set K=K+1 3. Exit
[End of Step 2 loop]
5. Exit
Exercise

• Consider the array AUTO which records the number of automobiles


sold each year from 2000 through 2010

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
Insert item into a sorted array
• Insert Sorted ( ):
Description: Here A is a sorted linear array (in ascending order) with N elements. ITEM is
the value to be inserted.
1. Set I = N  [Initialize counter]
2. Repeat While (ITEM < A [I]) and (I >= 1)
3. Set A [I+1] = A [I]  [Move elements downward]
4. Set I = I – 1 [Decrease counter by 1]
    [End of While Loop]
5. Set A [I+1] = ITEM [Insert element]
6. Set N = N + 1  [Reset N]
7. Exit
• Explanation: Here A is a sorted array stored in memory. This algorithm inserts a data
element ITEM into the (I + 1)th position in an array A. I is initialized from N i.e. from total
number of elements. ITEM is compared with each element until it finds an element which
is smaller than A[I] or it reaches the first element. During this process, the elements are
moved downwards and I is decremented. When it finds an element smaller then ITEM, it
inserts it in the next location i.e. I + 1 because I will be one position less where ITEM is to
be inserted. And finally, total number of elements is increased by 1.
To delete an item from an array
• Delete ( ):
Description: Here A is a linear array with N elements. LOC is the
location from where ITEM is to be deleted.

1. Set ITEM = A [LOC] [Assign the element to be deleted to ITEM]


2. Repeat For I = LOC to N
3. Set A [I] = A [I+1]  [Move the Ith element upwards]
    [End of For Loop]
4. Set N = N – 1 [Reset N]
5. Exit

• Explanation: First, the element to be deleted is assigned to ITEM from


location A [LOC]. Then I is set to LOC from where ITEM is to be
deleted and it iterated to total number of elements i.e. N. During this
loop, the elements are moved upwards. And lastly, total number of
elements is decreased by 1.
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] A[1,4]
2. A[2,1] A[2,2] A[2,3] A[2,4]
3. A[3,1] A[3,2] A[3,3] A[3,4]
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 [2,2]
[3,2]
r2
[1,3] [2,3]

[2,3] C3 [2,4]

[3,3] [3,1]

[1,4] [3,2]

[3,3] r3
[2,4] C4
[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]

You might also like