Chapter 2 - Arrays: Tran Thanh Tung
Chapter 2 - Arrays: Tran Thanh Tung
Arrays
Tran Thanh Tung
1
2
Objective
Basics of Array in Java
Linear search – Binary search
Storing objects
Big O notation
3
Introduction
How do we store list of integer entered by
user?
int num1;
int num2;
int num3;
Introduction
Array is the most commonly used data
structure
Built into most of the programming
languages
123 412 19 20
5
Definition
An ARRAY is a collection of variables all of
the same TYPE
Element
Index / positions
In Java
123 412 19 20
index 0 1 2 3 4
6
A 123 412 19 20
index 0 1 2 3 4
7
Example in Java
int A[];
int A1[] = new int[100];
int A2[] = new int { 1, 7, 9, 20};
Operations on array
Insertion
Searching
Deletion
(Demo on Array Workshop applet)
Duplication issue
Delete an item
10
Multi-dimension array
A matrix 1 0 3 4
5 1 32 12
6 7 1 10
19 5 4 1
11
Two-dimension array
Declaration
int [][] matrix = new int
[ROWS][COLUMNS];
int [][] matrix2 =
{
{1, 2, 3},
{6, 1, 4},
{9, 5, 1}
};
Accessing
matrix[0][10];
12
A 123 412 19 20 25
index 0 1 2 3 4
Stepthrough the array
Comparing searchKey with each element.
Reach the end but don’t found matched
element
Can’t find
13
Ordered arrays
Data items are arranged in order of key value.
A 412 123 25 20 19
index 0 1 2 3 4
A2 19 20 25 123 412
Index 0 1 2 3 4
14
A 412 123 25 20 19
index 0 1 2 3 4
A2 19 20 25 123 412
Index 0 1 2 3 4
15
?46
Smaller Larger
16
Binary search
Data structures and algorithms in Java – p.57
18
19
In class work
Modify
the Binary search algorithm for a
descending array
20
Advantage of ordered
arrays
Searching time : Good
Inserting time : Not good
Deleting time : Not good
Useful when
Searches are frequent
Insertions and deletions are not
21
Logarithm
Binary search
Log2n Range Comparisons needed
10 4
100 7
1,000 10
10,000 14
100,000 17
1,000,000 20
10,000,000 24
100,000,000 27
1,000,000,000 30
22
Must known
2i n log2n 2i n log2n
20 1 0 26 64 6
21 2 1 27 128 7
22 4 2 28 256 8
23 8 3 29 512 9
24 16 4 210 1024 10
25 32 5 211 2048 11
23
Storing objects
We need to
Store a collections of Students
Search student by Student name
Insert a new student, delete a student
In class work:
Read the sample code in p.65-69
24
Big O notation
Tomeasure the EFFICIENCY of algorithms
Some notions
Constant
Proportional to N
Proportional to log(N)
Big
O relationships between time and
number of items
25
O(1) – constant
Thetime needed by the algorithm is not
depend in size of items
Example
Insertion in an unordered array
Any others ?
26
O(N) – Proportional to N
Linear search of items in an array of N
items
On average T = K * N /2
For
an array of N’ items
If N’ = 2 * N
Then T’ = 2 * T
27
O(log N) - Proportional to
log(N)
For binary search:
T = K * log2(N)
O(N2)
O(N)
O(log N)
O(1)
29
Summary
Arraysin Java are objects, created with new
operator
Unordered arrays offer
fast insertion,
but slow searching and deletion
Binary search can be applied to an ordered
array
Big O notation provides a convenient way to
compare the speed of algorithms
An algorithm that runs in O(1) is the best, O(log N)
is good, O(N) is fair and O(N2) is pretty bad