You are on page 1of 33

1

@ 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.

Arrays
(Chapter 3)

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.

Outline of Chapter 3
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

1. Introduction

Definition of Array
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.

Data
values
of type:
int

10 9

Ref: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Declaring Array in Java


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]);
}
Ref: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Array Dimensions
Arrays could be of

one-dimension,
two dimension,
three-dimension or
in general multidimension.

This is reflected by
the number of
index ranges

Examples:
Grades[i] - 1-dim
Table[i] [j] - 2-dim
Bus[i] [j] [k] - 3-dim

Declaring & Initializing Array

1-dim array

Declaring & Initializing 2-D Array

2-dim array

Array Operations (store & retrieve)


store
//
ARRAY
: initialize
OPERATIONS
first element
- An array when viewed as a data
structure supports only two operations viz.,
anArray[0]
= 100;

storage of values (i.e.) writing into an array


// initialize
all
element
s (store)

(STORE
(a, i, e))

retrieval
of values (i.e.) reading from an array
For (i=0;
i<10;
i++)

(RETRIEVE (a, 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

Exercises
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) .

11

Q? Given an array, eg.,


Grade[10 : 100],
how many elements are
there?

12

Number Of Elements In An Array


Given 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


Array

14

2. Representation of Arrays
in Memory

15

Array Representation in Memory


The arrays are stored in memory in one of the two
ways, viz.,
row major order or lexicographic order or
column major order.

Column-major
Row-major

16

Array Representation in Memory


One dimensional array

17

Two dimensional array

18

Three dimensional array

19

N Dimensional arrays
Let be an N-dimensional array. The address calculation for the
retrieval of various elements are as given below:

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

Sparse matrix
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.

24

Sparse matrix
This 6X6 matrix can
be represented by
3X3 matrix

T3[1:6,1:6]

10

Col

Value

Row

25

Application#2: Ordered lists


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.

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

ADT for Arrays


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] = VAL
Retrieve the value in the ith element of array ARRAY
as
VAL = ARRAY[i]
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


on 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

@ 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 of
Arrays
(Chapter 3)

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.

You might also like