arrays - nathaniel g. martinds.nathanielgmartin.com/wk01/w1l3-arrays.pdf · 2016. 12. 30. · g e o...
TRANSCRIPT
![Page 1: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/1.jpg)
ARRAYS
![Page 2: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/2.jpg)
Week 1 2
INTRODUCTION
o An Array is a Data Structure with whichwe can perform operations on a collection ofsimilar data type such as simple list ortables of information.
o All elements in the array are of same typei.e. int, char, float etc.
![Page 3: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/3.jpg)
Week 1 3
o The individual elements within the arraycan be accessed by the integer calledIndex.
o eg-: int array[9]
o Length of an Array=UB-LB+1
9 8 1 -1 4 2 6 15 3
0 1 2 3 4 5 6 7 8
(Lower Bound)LB UB
(Upper Bound)
![Page 4: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/4.jpg)
ManavRach
naColl
egeof
Engg.
4
oThe zeroth element (9) in the list can be accessedby array[0].
oAn Index is also called a Subscript.
oTherefore, individual elements of an Array arecalled subscripted variables eg. array[0], array[1]etc.
oThus, an Array can be defined as a finite orderedcollection of items of same type.
![Page 5: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/5.jpg)
ManavRach
naColl
egeof
Engg.
5
Types of Array
ARRAYS
One DimensionalArray
Multi DimensionalArray
( An Array whose elements arespecified by a single subscriptEg. A[0] )
( An Array whoseelements are specifiedby two or more thantwo subscriptsEg. A[10][10] is a twodimensional array )
![Page 6: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/6.jpg)
Week 1 6
One Dimensional Array-:
o1-D Arrays are suitable for processing listsof items of identical types.
oThey are very useful for problems thatrequire the same operation to be performedon a group of data items.
![Page 7: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/7.jpg)
Week 1 7
Operations defined on 1-D Arrays-:
1. TRAVERSAL-: An Array can be traversed by visiting its
element starting from the zeroth element to the last element in the list.
Processing of the visited element can be
done as per the requirement of the problem.
![Page 8: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/8.jpg)
Week 1 8
Algorithm Traversal (A [], N){ //Description:- A is an array of size N. 1. for (i=0 to N-1) 1.1 Print A[i]}
//Time Complexity-: O(N)
![Page 9: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/9.jpg)
Week 1 9
2. INSERTION of N elements in an array
Algorithm Insertion (A [], N){//Description:- N is the number of elements tobe inserted in array A. 1. for( i=0 to N-1) 1.1 Enter A[i]}
//Time Complexity-: O(N)
![Page 10: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/10.jpg)
Week 1 10
3. INSERTION of an element at aparticular position-:
0 1 2 3 4 5 6 7
Insert 40 at location 4th
![Page 11: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/11.jpg)
Week 1 11
After Insertion-:
10 20 30 40 50 60 70 0 1 2 3 4 5 6 7
Inserted
m+1
N
![Page 12: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/12.jpg)
Week 1 12
Algorithm InsertionAtPos(A[], N, val, loc){ Description-: A is an array having N elements.val is the value to be inserted at location loc. 1. if (loc>N) 4.1 Print Insertion Not Possible 4.2 Exit 2. for (i=N-1 to loc-1) 2.1 A[i+1] = A[i] 3. A[loc-1] = val 4. N=N+1}
//Time Complexity-: O(N)
![Page 13: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/13.jpg)
13
4. Deletion of an element from an array-:
Delete an element at location 3rd
10 20 40 50 0
0 1 2 3 4
0 1 2 3 4
N
A[N-1]
![Page 14: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/14.jpg)
14
Algorithm Deletion(A[],N, loc){ Description-: A is an Array of size N loc is the location of deletion.1. if (loc>N) 1.1 Print Deletion Not Possible 1.2 Exit2.for (i=loc to N-1) 2.1 A[i-1]=A[i]3. N=N-1}
Time Complexity-: O(N)
![Page 15: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/15.jpg)
Week 1 15
ADDRESS CALCULATION-:oOne Dimensional Array-:
A B C D E 0 1 2 3 4
List[5]
Logical View-:
A
B
C
D
E
100
100+1
100+2
100+3
100+4
01
2
3
4
LogicalAddress
PhysicalAddress
![Page 16: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/16.jpg)
16
oThe array elements are stored in contiguous memorylocations by sequential allocation techniques.oThe Address of ith element of the array can beobtained if we know-:
1. The starting address i.e. the address of thefirst element called Base Address denoted byB
2. The size of the element in the array denoted byW
Address of List [i] = B + ( i – LB ) * W where LB is Lower Bound of the array
List[5] = List[0---4] ; LB=0
![Page 17: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/17.jpg)
Week 1 17
oTwo Dimensional Array-:
A [m] [n] represents an array A where m is no.ofrows and n is no.of columns.
Eg-:
4 5 6
7 8 9
0 1 2
0
1
A[2][3]
m*n1*2
![Page 18: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/18.jpg)
Week 1 18
Row Major Order-:
4
5
6
7
8
9
100
100+1
100+2100+3
100+4
100+5
0,0
0,10,2
1.01,1
1,2
In row-majorstorage, amultidimensionalarray in linearmemory isaccessed such thatrows are storedone after theother.
![Page 19: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/19.jpg)
19
Address of A [i] [j]= B + [(i-LBR)*n + (j-LBC)]*W
where :•LBR is the Lower Bound of Rows
•LBC is the Lower Bound of Columns
•W is the size of element
•n is the no.of columns
•Eg-: A[1][2]; m=2;n=3;LBR=0;LBC=0A [1] [2] = 100 + ( (1-0) * 3 + (2-0) ) *1 = 105
![Page 20: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/20.jpg)
Week 1 20
Column Major Order-:
4
7
5
8
6
9
100
100+1100+2
100+3
100+4
100+5
0,01,0
0,1
1,1
0,21,2
In column-majorstorage, amultidimensionalarray in linearmemory is accessedsuch that columnsare stored one afterthe other.
![Page 21: ARRAYS - Nathaniel G. Martinds.nathanielgmartin.com/wk01/W1L3-Arrays.pdf · 2016. 12. 30. · g e o f E n g g. 5 Types of Array ARRAYS One Dimensional Array Multi Dimensional Array](https://reader036.vdocuments.us/reader036/viewer/2022071603/613e05d359df6428461643df/html5/thumbnails/21.jpg)
Week 1 21
Address of A [i] [j] = B+ [(i-LBR) + (j-LBC)*m]*W
where •LBR is the Lower Bound of Rows
•LBC is the Lower Bound of Columns
•W is the size of element
•m is the no.of rows