pai ch03 array
DESCRIPTION
array javaTRANSCRIPT
1
PROPRIETARY MATERIAL. © 2008 The McGraw-Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without permission.
@ McGraw-Hill Education
Copyrighted Material -Additional resource material supplied with the book Data Structures and Algorithms : Concepts, Techniques and Applications authored by G.A.V. Pai and published by Tata McGraw Hill. This resource material is for Instructor's use only.
ArraysArrays(Chapter 3)(Chapter 3)
2
1. Introduction• Definition• Array Operations• Number of Elements in an array
• One-dimensional array• Two dimensional array• Multi-dimensional arrays
2. Representation of Arrays in Memory• One-dimensional array• Two-dimensional arrays• Three dimensional arrays• N-dimensional array
3. Applications• Sparse matrix • Ordered lists
4.ADT for arrays
Outline of Chapter 3
3
1. Introduction
4
An array is an ADT whose objects are sequence of elements of the same type and
the two operations performed on it are store and retrieve.
Definition of ArrayDefinition of Array
1 10 9Data valuesof type: int
Ref: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
5
class ArrayDemo { public static void main(String[] args) { // declares an array of integers int[] anArray; // allocates memory for 10 integers anArray = new int[10]; // initialize first element anArray[0] = 100; // prints the value of first element of array System.out.println("Element at index 0: " + anArray[0]);}
Declaring Array in JavaDeclaring Array in Java
Ref: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
6
Arrays could be of one-dimension, two dimension, three-dimension or in general multi-
dimension. This is reflected
by the number of index ranges
Array DimensionsArray Dimensions
Examples:Grades[i] - 1-dimTable[i] [j] - 2-dimBus[i] [j] [k] - 3-dim
7
Declaring & Initializing ArrayDeclaring & Initializing Array
1-dim array
8
Declaring & Initializing 2-D ArrayDeclaring & Initializing 2-D Array
2-dim array
9
ARRAY OPERATIONS - An array when viewed as a data structure supports only two operations viz.,
storage of values (i.e.) writing into an array (STORE (a, i, e))
retrieval of values (i.e.) reading from an array (RETRIEVE (a, i))
Array Operations (store & retrieve)Array Operations (store & retrieve)// store : initialize first elementanArray[0] = 100; // initialize all element s (store)For (i=0; i<10; i++)
anArray[i] = 0; // save value in array elements
// retrieve : prints the value of first element of array
System.out.println("Element at index 0: " + anArray[0]);
Sum=0;For (i=0; i<10; i++)
Sum += anArray[i]; // sum up all values
10
1. Declare in Java the following array variables:• 150 grades of real number• 150 students names• Bus seats in 2-dimensional array of type Boolean.
Assume we have 4 seats in one row with maximum 10 rows. Initially what are the values of the bus?
• As in Q#3 but to store seats for 10 buses• Find out the problem of Game of Life in the Internet.
How do we represent the data structure?2. Find out the solution to sort 150 students info (with
names, age and GPA) .
Exercises
11
Q? Given an array, eg.,Grade[10 : 100],
how many elements are there?
12
Number Of Elements In An ArrayNumber Of Elements In An ArrayGiven an array A[l : u] where l is the lower bound and u is the upper bound of the index range, then:
• the number of elements in A is given by (u – l + 1).
A[l1 : u1, l2:u2] has a size of (u1-l1+1) (u2-l2+1) elements.
Examples:
Grades[0 : 9] (9-0+1)
l=0 u=9
13
Number Of Elements In An n-Dim Number Of Elements In An n-Dim ArrayArray
A multi-dimensional array
A[1: u1, 1:u2, … 1: un] has a size of
nuuu .... 21 elements, (i.e.) i
n
iu
1 .
Generalizing,
the array A ]:...:,:,:[ 332211 nn ulululul has a size
of )1(1
ii
n
ilu elements.
14
2. Representation of Arrays in Memory
15
The arrays are stored in memory in one of the two ways, viz.,
row major order or lexicographic order or column major order.
Array Representation in Memory
Row-majorColumn-major
16
For the array A(0:n) let be the address of the starting memory location
referred to as the base address of the array. A[0] occupies the memory location : address is , A[1] occupies +1 and so on. In general, the address of A[i] is given by +i. Note: In Java the index starts with 0.
Array Representation in MemoryOne dimensional array
17
Consider the array A[1:u1, 1:u2] which is to be stored in memory. It is helpful to imagine this array as u1 number of one-dimensional arrays of length u2.
In general, for a two-dimensional array ]:,:[ 2211 ululA the
address of A[i, j] is given by ).()1)(( 2221 ljluli
Two dimensional array
Thus if A[1,1] is stored in address , the base address, then A[i,1] has address 2)1( ui , and A[i, j] has address ).1()1( 2 jui
Try A[1:3,1:2] – A[1,1],A[1,2],A[2,1],A[2,2],A[3,1],A[3,2]
α α+1 α+2 α+3 α+4 α+5
18
Three dimensional arrayFor the three dimensional array
]:1,:1,:1[ 321 uuuA , the address of ]1,1,[iA would be 32 .)1( uui , the address of ]1,,[ jiA would be
332 )1()1( ujuui , and the address of ],,[ kjiA would be
).1()1()1( 332 kujuui
In general for a three-dimensional array ]:,:,:[ 332211 ulululA the address of ],,[ kjiA is
given by ).()1)(()1)(1)(( 333233221 lkluljlululi
19
N Dimensional arraysLet be an N-dimensional array. The address calculation for the retrieval of various elements are as given below:
Element Address
]1,......1,1,[ 1iA Nuuui ..)1( 321
]1,...1,1,,[ 21 iiA NN uuuiuuui ....)1(..)1( 432321
]1,..1,1,1,,,[ 321 iiiA NNN uuuiuuuiuuui ...)1(..)1(..)1( 543432321
… … …
],...,,[ 321 NiiiiA
)1(.....)1(..)1( 432321 NNN iuuuiuuui
=
N
jjj ai
1)1( where ja ju
N
jkk
1,
1 N
20
3. Applications
21
A Matrix is : a mathematical object which finds its
applications in various scientific problems. an arrangement of m X n elements arranged
as m rows and n columns.
22
The Sparse matrix is a matrix with zeros as the dominating elements.
Issue: A matrix consumes a lot of space in memory.
A 1000 X 1000 matrix needs 1 million storage locations in memory.
Imagine the situation when the matrix is sparse!
To store a handful of non-zero elements, voluminous memory is allotted and there by wasted!
23
To save valuable storage space,
we resort to a 3-tuple representation viz., (i, j, value) to represent each non-zero element of the sparse matrix.
In other words,
a sparse matrix is represented by another matrix B[0:t, 1:3] with t+1 rows and 3 columns.
Here t refers to the number of non-zero elements in the sparse matrix.
While rows 1 to t record the details pertaining to the non-zero elements as triple(that is 3 columns), the zeroth row viz. B[0,1]=no of rows of the original sparse matrix A ,
B[0,2]=no of columns of the original sparse matrix A
and B[0,3] record the number of non-zero elements.
Sparse matrix
24
Sparse matrix
6 6 2
5 1 46 2 10
Row Col Value
This 6X6 matrix can be represented by
3X3 matrix
T3[1:6,1:6]
25
One of the simplest and useful data objects in computer science is an ordered list or linear list. An ordered list can be either empty or non empty. In the latter case, the elements of the list are known as atoms, chosen from a set D. The ordered lists provide a variety of operations such as retrieval, insertion, deletion, update etc. The most common way to represent an ordered list is by using a one-dimensional array. Such a representation is termed sequential mapping through better forms of representation have been presented in the literature.
Application#2: Ordered lists
26
Example:
Week[1:7]=(sun,mon,tue,sat, , , )
Insert (wed)-> week[1:7]=(sun,mon,tue,wed,sat, , )Delete (sat) -> week[1:7]=(sun,mon,tue,wed, , , )
27
4. ADT for Arrays
28
Data objects:• A set of elements of the same type stored in a sequence
2 basic operations: Store value VAL in the ith element of the array ARRAY
ARRAY[i] = VALRetrieve the value in the ith element of array ARRAY as
VAL = ARRAY[i]
ADT for Arrays
We may add more operations @ methods to this ADT
29
Implementing ADTs in Java// File#1: Define @ specify the interface
public interface MYarrays { void Store (int xPos, int yPos, int VAL);
int Retrieve (int xPos, int yPos);
…more methods and fields…}
// File#2: Implement all methods
public class Test implements MYarrays {… all required methods of all interfaces implemented …}
30
More on ADTs for Java?
Review on Java Interface
31
Reviews
32
Quick Online Review Quick Online Review
on Arrayson Arrays
On Arrays:http://www.ibm.com/developerworks/
library/j-arrays/index.html
On Java Collections:http://docs.oracle.com/javase/1.3/docs/guide/collections/reference.html
33
PROPRIETARY MATERIAL. © 2008 The McGraw-Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without permission.
@ McGraw-Hill Education
Copyrighted Material -Additional resource material supplied with the book Data Structures and Algorithms : Concepts, Techniques and Applications authored by G.A.V. Pai and published by Tata McGraw Hill. This resource material is for Instructor's use only.
End ofEnd ofArraysArrays
(Chapter 3)(Chapter 3)