Meljun Cortes Data Structures Arrays
-
Upload
meljun-cortes-mbampa -
Category
Documents
-
view
231 -
download
0
Transcript of Meljun Cortes Data Structures Arrays
-
8/21/2019 Meljun Cortes Data Structures Arrays
1/21
Data Structures
Introduction
LIST
ordered set of a variable number of elements to
which additions and deletions may be made
one of the simplest and most commonly foundtype of data
LINEAR LIST
list which displays the relationship of physicaladjacency
Arrays *Property of STI Page 1 of 21
finite sequence of simple data, items, orrecords
Example
Days of the Week (Monday, Tuesday, Wednesday,Thursday, Friday, Saturday,Sunday)
Values in a Deck ofCards
(2, 3, 4, 5, 6, 7, 8, 9, 10, Jack,Queen, King, Ace)
Floors of a Building Basement, Lobby, Mezzanine,First, Second, Third
-
8/21/2019 Meljun Cortes Data Structures Arrays
2/21
Data Structures
Arrays
ordered collection of data items of the sametype referred to collectively by a single name
Index – each variable or cell in an array
Elements – individual data / items in an array
indicated by the array name followed by itsdimensions appears in a square brackets
Arrays *Property of STI Page 2 of 21
Dimensions – an integer from 1 – n called
dimensioned variables
arrayName [0][0]
-
8/21/2019 Meljun Cortes Data Structures Arrays
3/21
Data Structures
Operations in Arrays
Arrays *Property of STI Page 3 of 21
Figure 1 Array
Insert
Search Delete
-
8/21/2019 Meljun Cortes Data Structures Arrays
4/21
Data Structures
Operations in Arrays
Insert
Adds item to the indicated place/cell
The process is very fast because it only includesone step
Every data inserted is placed in the first vacantcell
Search
Another process that the algorithm carries out
The red arrow from the figure states where thesearch starts
Arrays *Property of STI Page 4 of 21
oves me o ca y ownwar s o searc or eitem
Delete
The algorithm must first locate the item to deleteit
The deleted item causes hole in the array
Hole - one or more cells that have filled cellsabove them
02 14 46 65 17 48 32 09 40 08
0 1 2 3 4 5 6 7 8 9
02 14 46 17 48 32 09 40
0 1 2 3 4 5 6 7 8
08
Item to be deleted
-
8/21/2019 Meljun Cortes Data Structures Arrays
5/21
Data Structures
Basics of Arrays in Java
Arrays are treated as primitive type, they are treatedas objects
use the new operator to declare an array
int[] intArray; // declares a reference to
an arrayintArray = new int[100]; // initialize the array, and
sets intArray to
refer to it
Arrays *Property of STI Page 5 of 21
intArray name is a reference to an array and not
the array itself The length field of an array can be used to find the
size in bytes:
the size of an array, when created, can no longer bemodified
int arrayLength =
intArray.length; //find array
length
-
8/21/2019 Meljun Cortes Data Structures Arrays
6/21
Data Structures
Basics of Arrays in Java
Array elements can be accessed through the use ofsquare brackets
temp = intArray[6]; // get contents of seventhelement of array
intArray[7] = 48; // insert 48 into the eightcell
Arrays *Property of STI Page 6 of 21
the first element is numbered 0 so that the indices inan array of 10 elements run from 0 to 9
initialize first an array before attempting to access it toavoid syntax error
can initiate an array of primitive type to somethingbesides 0 using:
int[] intArray = {0, 2, 4, 6, 8, 10,
12, 14, 16, 18};
Syntax below gives you a runtime error
autoData[] carArray = new
autoData[4000];
-
8/21/2019 Meljun Cortes Data Structures Arrays
7/21
Data Structures
Exercise
Arrays *Property of STI Page 7 of 21
-
8/21/2019 Meljun Cortes Data Structures Arrays
8/21
Data Structures
One-dimensional Array
Array1[j]
Two-dimensional Array
Array1[i][j]
Multi-dimensional Array
Array[i][j][k]…
Dimensionality of an Array
Arrays *Property of STI Page 8 of 21
-
8/21/2019 Meljun Cortes Data Structures Arrays
9/21
Data Structures
It is the basic array
A vertical table with number of columns andonly one row
type[] variableName;
An array of ints
int[] intArray;
One-Dimensional Array
Arrays *Property of STI Page 9 of 21
Random[] randomArray;
Arrays are fixed-length structure
What is the difference of the two array syntaxbelow?
int[] intArray = new intArray[5];
and
int size = 5;
Random[] randomArray = new
Random[size];
-
8/21/2019 Meljun Cortes Data Structures Arrays
10/21
Data Structures
One-Dimensional Array
Index and Length
Indices – used to access each location ofarrays from 0 ranging up to the instantiated
array
An array of length 10, instantiated asint[] intArray = new intArray[10]
Attempting to access an index which is out ofrange, gives you
Arrays *Property of STI Page 10 of 21
rray n ex u oun s xcep on
Example
int size = 5;
int[] intArray = new int[size];
for(int index = 0; index < size;
index++)
{ intArray[index] = index; }
-
8/21/2019 Meljun Cortes Data Structures Arrays
11/21
Data Structures
One-Dimensional Array
Changing the Size of an Array
Analyze the syntax below and give its output:
int[] intArray = new int[5];
for (int index = 0; index < 3;index ++)
{ intArray[index] = index + 1; }
Arrays *Property of STI Page 11 of 21
ow o ma e your array grow an s r n
create a temporary array of the desired size
using for loop, access each item from theoriginal array and save it to thecorresponding index of the temporary array
save the temporary array into the variableof your original array
-
8/21/2019 Meljun Cortes Data Structures Arrays
12/21
Data Structures
One-Dimensional Array
Example (continued..)
int[] intArray = new int[5];
for (int index = 0; index < 3;
index ++){ intArray[index] = index + 1; }
int[] tempArray = new int[3];
for (int index = 0; index <
Arrays *Property of STI Page 12 of 21
tempArray.length; index++)
{ tempArray[index] =
intArray[index]; }
intArray = tempArray;
-
8/21/2019 Meljun Cortes Data Structures Arrays
13/21
Data Structures
Two-Dimensional Array
It is an array of an array
Also called as tables or matrices
Row – first dimension
Column – second dimension
Arr1[2][3]
Arrays *Property of STI Page 13 of 21
Two-Dimensional Array with 3 Rows and 4 Columns
-
8/21/2019 Meljun Cortes Data Structures Arrays
14/21
Data Structures
Two-Dimensional array
declaration and instantiation of 2D array is similar tothat of 1D array
int rows = 3, cols = 4;
int[][] tableArray = new int[rows][cols];
for (int rowIndex = 0; rowIndex < rows;
rowIndex ++)
{for (int colIndex = 0; colIndex <
cols; colIndex ++)
{ tableArray[rowIndex][colIndex] =
rowIndex * colIndex + 1; }
Arrays *Property of STI Page 14 of 21
Curly braces is the alternative constructor for 2Darrays
int[][] tableArray = { {1,2,3,4,5},
{6,7,8,9,0} };
For a more look of like a table
int[][] tableArray = { {1,2,3,4,5} }
{ {6,7,8,9,0} };
You can initialize the row dimension without the
columns but not vice versaint[][] tableArray = new int[5][]; //
illegalint[][]
tableArray = new int[][5]; // accepted
-
8/21/2019 Meljun Cortes Data Structures Arrays
15/21
Data Structures
Two-Dimensional Array
example of a 2D array
Arrays *Property of STI Page 15 of 21
-
8/21/2019 Meljun Cortes Data Structures Arrays
16/21
Data Structures
Two-Dimensional Array
Matrix - is a mathematical object which arisesin many physical problems
consists of m rows and n columns
Arrays *Property of STI Page 16 of 21
m x n (read as m by n) is written todesignate a matrix with m rows and n columns
-
8/21/2019 Meljun Cortes Data Structures Arrays
17/21
Data Structures
Two-Dimensional Array
Magic Square – is an n x n matrix of theintegers 1 to n2
Arrays *Property of STI Page 17 of 21
Algorithm to create a magic square by H.Coxeter
Begin with 1 in the middle of the top row Move up and left assigning numbers in
increasing order to empty squares
If you fall off the square imagine the samesquare as tilting the plane and continue
If a square is occupied, move down instead
and continue
-
8/21/2019 Meljun Cortes Data Structures Arrays
18/21
Data Structures
Two-Dimensional Array
Sample program of Magic Square
Arrays *Property of STI Page 18 of 21
-
8/21/2019 Meljun Cortes Data Structures Arrays
19/21
Data Structures
Two-Dimensional Array
Transpose – another operation that can beperformed on matrices that computes for thetranspose of matrix
the elements in the [i,j ] position aretransferred to the [ j,i ] position
elements in the diagonal will always remainin the same position since i = j
Arrays *Property of STI Page 19 of 21
Arr2 matrix
Arr2 transpose matrix
-
8/21/2019 Meljun Cortes Data Structures Arrays
20/21
Data Structures
Linear Search
checks every element of a list one at a time insequence
also called as sequential search
Algorithm for linear search
Create an ordered array
Initialize a target value or the key
Arrays *Property of STI Page 20 of 21
If index == key, return true
If index < key, return false
Ordered array
-
8/21/2019 Meljun Cortes Data Structures Arrays
21/21
Data Structures
Binary Search
Usually coded as
mid = (high + low) /2;
Algorithm for binary search
Array elements should be sorted
Find the middle Compare the middle item to the target value or
the key
If not found - read, search parameters, half thesize and start over
Arrays *Property of STI P 21 f 21
If found, return