Handout Arrays
Handout 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
Arrays
1 Introduction
Arrays Arrays
Linear Arrays Linear Arrays
Example
1 Introduction
Arrays Arrays
Linear Arrays Linear Arrays
Representation of Liniear Arrays in Memory Travarsing Linear Arrays
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)
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
7. Exit
Arrays Arrays
Linear Arrays Linear Arrays
Searching Searching
1 Introduction
1 Introduction
Arrays Arrays
Linear Arrays Linear Arrays
Searching Searching
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
Arrays Arrays
Linear Arrays Linear Arrays
Searching Searching
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
Arrays Arrays
Linear Arrays Linear Arrays
Multidimensional Arrays Multidimensional Arrays
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
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)
Complexity: C = m.n.p
Arrays Arrays
Linear Arrays Linear Arrays
Matrices Matrices
Sparse matrices I
1 Introduction
Arrays Arrays
Linear Arrays Linear Arrays
Matrices Matrices
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
Arrays Arrays
Linear Arrays Linear Arrays
Matrices Matrices
Arrays Arrays
Linear Arrays Linear Arrays
Matrices Matrices