0% found this document useful (0 votes)
11 views

Handout Arrays

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

Handout Arrays

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

Arrays Arrays

Outline I

1 Introduction

Arrays
2 Linear Arrays
Representation of Liniear Arrays in Memory
Travarsing Linear Arrays
Inserting and Deleting
Searching
Linear Search
Binary Search
Multidimensional Arrays
Matrices
Sparse Matrices

Arrays Arrays
Introduction Introduction

Classification
1 Introduction

2 Linear Arrays Data structures are classified as either linear or nonlinear


Representation of Liniear Arrays in Memory
Travarsing Linear Arrays A data structure is said to be linear if its elements form a linear
Inserting and Deleting list
Searching I Linear - Arrays and Linked lists
Linear Search I Nonlinear - Trees and Graphs
Binary Search
Multidimensional Arrays
Matrices
Sparse Matrices
Arrays Arrays
Introduction Linear Arrays

Arrays
1 Introduction

In arrays linear relationship between elements is represented by


means of sequential memory locations. 2 Linear Arrays
Representation of Liniear Arrays in Memory
The following are the operations on any linear structure: Travarsing Linear Arrays
I Traversal Inserting and Deleting
I Search Searching
I Insertion and Deletion Linear Search
I Sorting Binary Search
I Merging Multidimensional Arrays
Matrices
Sparse Matrices

Arrays Arrays
Linear Arrays Linear Arrays

Linear Arrays I Linear Arrays II

A linear array is a list of a finite number n of homogenous data


elements such that The elements of the array can be denoted by
1. The elements of the array are referenced respectively by an
I Parentheses notation
index set consisting of n consecutive numbers
2. The elements of the array are stored respectively in succes- A(1), A(2), A(3), · · · A(n)
sive memory locations I Bracket notation
A[1], A[2], A[3], · · · A[n]
The number n of elements is called length or size of the array. I Subscript notation
Length can also be calculated by
A1 , A2 , A3 , · · · An
length=upperbound-lowerbound+1

where upperbound is the largest index and lowerbound is the


smallest index of the array
Arrays Arrays
Linear Arrays Linear Arrays
Representation of Liniear Arrays in Memory

Example
1 Introduction

125 295 295 163 176 264 2 Linear Arrays


A
1 2 3 4 5 6 Representation of Liniear Arrays in Memory
Travarsing Linear Arrays
length=6 Inserting and Deleting
A[1]=125, A[2]=295 Searching
Linear Search
Binary Search
Multidimensional Arrays
Matrices
Sparse Matrices

Arrays Arrays
Linear Arrays Linear Arrays
Representation of Liniear Arrays in Memory Travarsing Linear Arrays

The elements of linear array are stored in successive memory 1 Introduction


cells
Accordingly computer does not keep track of address of every
element but tracks only address of first element
2 Linear Arrays
Let A be an array.The address of first element is denoted by Representation of Liniear Arrays in Memory
Base(A) Travarsing Linear Arrays
The computer calculates any address of A by using the formula Inserting and Deleting
Searching
LOC(A[K])=Base(A)+w(K-lower bound) Linear Search
Here w is number of words per memory cell for array A Binary Search
Multidimensional Arrays
Any element A[K] can be located and processed in a time inde-
Matrices
pendent of K. This is an important property of linear arrays Sparse Matrices
Arrays Arrays
Linear Arrays Linear Arrays
Travarsing Linear Arrays Travarsing Linear Arrays

Array Traversal I Array Traversal II

Let A be an array.Suppose we want to print the elements of A or we


want to count number of elements of A with a given property. This can
be accomplished by traversing This algorithm is an alternative form of the above algorithm which uses
Algorithm 1: Let LA be a linear array with lower bound LB and upper repeat for loop instead of repeat while loop
bound UB Algorithm 2:
1. Set K:=LB [Initialize counter] 1. Repeat for K=LB to UB:
2. Repeat steps 3 and 4 while K≤UB Apply PROCESS to LA[K]
End of loop
3. Apply PROCESS TO A[K] [Visit element]
2. Exit
4. Set K:=K+1 [Increase counter]
End of step 2 loop
5. Exit

Arrays Arrays
Linear Arrays Linear Arrays
Inserting and Deleting Inserting and Deleting

Let A be an array
1 Introduction Insertion refers to the operation of adding another element to A
Deletion refers to removing one of the elements of A
Inserting at the end can be easily done provided memory space
2 Linear Arrays allocated for array is large enough
Representation of Liniear Arrays in Memory
Travarsing Linear Arrays For insertion at the middle on an average half of the elements
Inserting and Deleting must be moved downward to new locations to accommodate ele-
Searching ment and keep order of other elements
Linear Search
Similarly deletion at end has no difficulty while deletion at the
Binary Search
middle requires each subsequent element be moved one location
Multidimensional Arrays
upward to fill array
Matrices
Sparse Matrices Here downward refers to location with larger subscripts and up-
ward refers to smaller subscripts
Arrays Arrays
Linear Arrays Linear Arrays
Inserting and Deleting Inserting and Deleting

Insertion Deletion
Insertion Algorithm
Deletion algorithm
Insert element ITEM into Kth position of LA
INSERT(LA, N, K, ITEM) Delete Kth element from LA and assign it to item
1. [Initialize counter.] Set J:=N DELETE(LA,N,K,ITEM)

2. Repeat steps 3 and 4 while J≥ K 1. Set ITEM:=LA[K]

3. [Move Jth element downward.] Set LA[J+1]:= LA[J] 2. Repeat for J=K to N-1 :
[Move J+1st element upward] Set LA[J]:=LA[J+1]
4. [Decrease counter.] Set J:= J-1
End of step 2 loop [End of loop]

5. [Insert element.] Set LA[K]:= ITEM 3. [Reset the number N of elements in LA] Set N:=N-1

6. [Reset N.] Set N:= N+1 4. Exit

7. Exit

Arrays Arrays
Linear Arrays Linear Arrays
Searching Searching

1 Introduction

1. Searching refers to finding location of a particular item in the


given data
2 Linear Arrays 2. Search is said to be successful if the item appears in the data
Representation of Liniear Arrays in Memory and unsuccessful otherwise
Travarsing Linear Arrays
Inserting and Deleting 3. There are many different search algorithms. The algorithm one
Searching chooses depends on the way data is organized
Linear Search
4. Two popular searching algorithms are linear and binary search
Binary Search
Multidimensional Arrays
Matrices
Sparse Matrices
Arrays Arrays
Linear Arrays Linear Arrays
Searching Searching

1 Introduction

Here in order to search for a given ITEM in DATA we compare


2 Linear Arrays
ITEM with each element of DATA
Representation of Liniear Arrays in Memory
Travarsing Linear Arrays This method which traverses DATA sequentially to locate ITEM is
Inserting and Deleting called linear search or sequential search
Searching
Linear Search
Binary Search
Multidimensional Arrays
Matrices
Sparse Matrices

Arrays Arrays
Linear Arrays Linear Arrays
Searching Searching

Algorithm1 A linear array DATA with N elements and a specific ITEM


of information are given. This algorith finds the location LOC of ITEM
in the array DATA or sets LOC=0. Algorithm2 This algorithm finds LOC of ITEM in DATA
1. [Initialize] Set K:=1 and LOC:=0. LINEAR(DATA,N,ITEM,LOC)

2. Repeat Steps 3 and 4 while LOC:=0 and k≤N. 1. [Insert ITEM at end of DATA] Set DATA[N+1]:=ITEM
3. If ITEM=DATA[K], then: Set LOC:=K. 2. [Initialize counter.] Set LOC:=1
4. Set k:=k+1.[Increments counter.] 3. [Search for ITEM.]
[End of Step 2 loop.] Repeat while DATA[LOC]6=ITEM
Set LOC:=LOC+1
5. [Successful?]
If LOC=0, then: [End of loop]
Write: ITEM is not in the array DATA. 4. [Successful?] If LOC=N+1, then: Set LOC:=0
Else: 5. Exit
Write: LOC is the location of ITEM.
[End of If structure.]
6. Exit.
Arrays Arrays
Linear Arrays Linear Arrays
Searching Searching

Complexity
1 Introduction

The complexity is measured by no. of comparisions required to 2 Linear Arrays


find ITEM in DATA. Representation of Liniear Arrays in Memory
Travarsing Linear Arrays
Clearly the worst case occurs when one must search through the Inserting and Deleting
entire array. Thus, worst case running time is proportional to n. Searching
What is the running time of the average case? Linear Search
Binary Search
Multidimensional Arrays
Matrices
Sparse Matrices

Arrays Arrays
Linear Arrays Linear Arrays
Searching Searching

Binary search Binary Search


Algorithm Here DATA is sorted LB is lower bound UB is upper bound.
Algorithm finds LOC of ITEM in DATA or sets LOC=NULL.
BINARY(DATA,LB,UB,ITEM,LOC)

1. [Initialize segment variables.]


Suppose data in an array is sorted in increasing numerical order Set BEG:=LB, END:=UB and MID=INT((BEG+END)/2).
then binary search is very efficient
2. Repeat steps 3 and 4 while DATA[MID]6=ITEM and BEG≤END.

Binary search works as follows: 3. If ITEM<DATA[MID] then:


Set END:=MID-1.
I Assign first=0 and last=N-1 Else:
Set BEG:=MID+1.
first + last
I Calculate Mid= [End of If structure.]
2
I If ITEM is less than A[mid] then make last=mid and repeat 4. Set MID:= INT ((BEG+END)/2).
[End of Step 2 loop.]
I Otherwise make first=mid and repeat
5. If DATA[MID]=ITEM, then:
Set LOC:=MID.
Else:
Set LOC:=NULL.
[End of If structure.]

6. Exit
Arrays Arrays
Linear Arrays Linear Arrays
Searching Searching

Complexity Limitation

Here we can observe that each comparison reduces the sample Binary search algorithm requires two conditions:
size in half,Hence the worst case time complexity will be equal to I The list must be sorted.
log2 n. I One must have direct access to the middle element in any
Hence binary search will be more efficient when the array is sorted. sublist.

Arrays Arrays
Linear Arrays Linear Arrays
Multidimensional Arrays Multidimensional Arrays

Multidimensional Arrays I
1 Introduction

Two-dimensional arrays
2 Linear Arrays A two dimensional array A is a collection of m.n data elements repre-
Representation of Liniear Arrays in Memory sented as A[J,K] such that 1≤J≤m and 1≤K≤n
Travarsing Linear Arrays  
Inserting and Deleting A[1, 1] A[1, 2] A[1, 3] A[1, 4]
Searching A[2, 1] A[2, 2] A[2, 3] A[2, 4]
Linear Search A[3, 1] A[3, 2] A[3, 3] A[3, 4]
Binary Search
Multidimensional Arrays Two dimensional array with 3 rows and 4 columns.
Matrices
Sparse Matrices
Arrays Arrays
Linear Arrays Linear Arrays
Multidimensional Arrays Multidimensional Arrays

Representation of Two-Dimensional Arrayas in Representation of Two-Dimensional Arrayas in


Memory I Memory II

The programming language will store the array A :


I Column-major order: Column by column.
I Row-major order: Row by row.

Arrays Arrays
Linear Arrays Linear Arrays
Multidimensional Arrays Multidimensional Arrays

Representation of Two-Dimensional Arrayas in General Multidimensional arrays I


Memory III

General Multidimensional array is a collection of m1 .m2 . · · · .mn


data elements.
Column-major order:
The element is denoted by A[k1 ][k2 ] · · · [kn ] such that
LOC(A[J, K]) = Base(A) + w[M[K - 1) + (J - 1)]
1 ≤ K1 ≤ m1 , 1 ≤ K2 ≤ m2 , · · · , 1 ≤ Kn ≤ mn .
Row-major order: Column-major order:
LOC(A[J, K]) = Base(A) + w[N[J - 1) + (K - 1)] LOC(A[K1 , K2 , · · · , Kn ]) = Base(A) + w[(((· · · (EN LN−1 + EN−1 )LN−2 ) + · · · + E3 )L2 + E2 )L1 + E1 ]

Row-major order:
LOC(A[K1 , K2 , · · · , Kn ]) = Base(A) + w[(((· · · (E1 L2 + E2 )L3 ) + E3 )L4 + · · · + EN−1 )LN + EN ]
Arrays Arrays
Linear Arrays Linear Arrays
Multidimensional Arrays Matrices

General Multidimensional arrays II


1 Introduction

Where, the length Li of dimension i of C is the number of ele-


ments in the index set, and Li can be4 calculated, as 2 Linear Arrays
Li = upper bound - lower bound + 1 Representation of Liniear Arrays in Memory
Travarsing Linear Arrays
and, for a given subscript Ki , the effective index Ei of Li is the
Inserting and Deleting
number of indeces preceding Ki in the index set, and Ei can be
Searching
calculated from Linear Search
Ei = ki - lower bound Binary Search
Multidimensional Arrays
Matrices
Sparse Matrices

Arrays Arrays
Linear Arrays Linear Arrays
Matrices Matrices

Matrices I Matrices II
Matrix multiplication Algorithm
Here we have A=m.p and B=p.n and C=A.B
MATMUL(A,B,C,M,P,N)

1. Repeat Steps 2 to 4 for I=1 to M:


Algebra of matrices
Suppose A is an m.p matrix and B is an p.n matrix product of A and B 2. Repeat Steps 3 and 4 for J=1 to N:
is m.n matrix whose Cij can be written as 3. Set C(I,J]:=0. [Initialize C(I,J].]

Pp 4. Repeat for K=1 to P:


Cij = Ai1 B1j + Ai2 B2j + · · · + Aip Bpj = k=1 Aik Bkj C(I,J]:=C(I,J]+A[I,K]*B[K,J]
[End of inner loop.]
[End of Step2 middle loop.]
[End of Step1 outer loop.]
5. Exit

Complexity: C = m.n.p
Arrays Arrays
Linear Arrays Linear Arrays
Matrices Matrices

Sparse matrices I
1 Introduction

2 Linear Arrays A general matrix consists of m rows and n columns of numbers.


Representation of Liniear Arrays in Memory We write m × n to designate a matrix with m rows and n columns.
Travarsing Linear Arrays Such a matrix has mn elements. When m = n, we call the matrix
Inserting and Deleting square.
Searching
Linear Search It is very normal to store a matrix in two-dimensional array, say
Binary Search A[1 : m, 1 : n]. Then, we can work with any element by writing
Multidimensional Arrays A(i, j).
Matrices
Sparse Matrices

Arrays Arrays
Linear Arrays Linear Arrays
Matrices Matrices

Sparse matrices II Sparse matrices III

The second matrix of the figure has many zero entries. Such a
matrix is called sparse.
In second matrix, only 8 out of 36 entries are non-zero and that Matrices with a relatively high proportion of zero entries are called
is sparse! sparse matrices for example lower and upper triangular matrices

Here one may save space by storing only those nonzero entries
Arrays Arrays
Linear Arrays Linear Arrays
Matrices Matrices

Representation of Sparse Matrix I Representation of Sparse Matrix II

A specific representation is used for sparse matrix which only


stores the non-zero elements.
Each element of a matrix is uniquely characterized by its row and
column position, say (i, j). Matrix is stored as a list of 3-tuples of
the form (i, j, value).
Sparse matrix A(0 : t, 1 : 3) where t = 8 is the number of non-
zero terms.
The element A(0, 1) and A(0, 2) contain the number of rows and
columns of the matrix. A(0, 3) contains the number of non-zero
terms.

Arrays Arrays
Linear Arrays Linear Arrays
Matrices Matrices

Representation of Sparse Matrix III Transpose of a Sparse Matrix I

Transpose operation can be applied over sparse matrix.


For example we want to represent a lower triangular matrix In transpose operation, we move the elements so that the ele-
ment in the i, j position gets put in the j, i position, i.e., inter-
I Suppose AJK is lower triangular matrix. Then we can store
changing row and column.
the nonzero entries in another array B
I For B[L]=AJK we want L in terms of J and K The elements of diagonal will remain unchanged, since i = j.
I Here elements upto Jth row are
1+2+...(J-1)=J(J-1)/2
I Then Kth element in Jth row is L=J(J-1)/2+K
I Thus L is represented in terms of J and K
Arrays Arrays
Linear Arrays Linear Arrays
Matrices Matrices

Transpose of a Sparse Matrix II Transpose of a Sparse Matrix III

Arrays Arrays
Linear Arrays Linear Arrays
Matrices Matrices

Transpose of a Sparse Matrix IV Product of Sparse Matrices I

For matrices A and B, where A is m×n and B is n×p, the product


matrixPC has dimension m × p. Its (i, j) element is defined as
cij = 1≤k ≤n aik bkj
For 1 ≤ i ≤ m and 1 ≤ j ≤ p, the product of two sparse matrices
may no longer be sparse, for instance,

You might also like