Lecture 02 - Arrays
Lecture 02 - Arrays
International University
School of Computer Science and Engineering
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 2
Objective
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 3
Introduction
int num1;
int num2;
int num3;
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 4
Introduction
123 412 19 20
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 5
Definition
123 412 19 20
index 0 1 2 3 4
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 6
Initialization
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 7
Accessing array elements
A 123 412 19 20
index 0 1 2 3 4
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 8
Example in Java
int A[];
int A1[] = new int[100];
int A2[] = new int { 1, 7, 9, 20};
+Insertion
+Searching
+Deletion
+Duplication issue
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 10
Delete an item
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 11
Multi-dimension array
+A matrix
1 0 3 4
5 1 32 12
6 7 1 10
19 5 4 1
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 12
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];
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 13
Linear searching technique
A 123 412 19 20 25
index 0 1 2 3 4
+Step through the array
+Comparing the SearchKey with each element.
+Reach the end but don’t find any matched element
→ Can’t find
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 14
Ordered arrays
A 412 123 25 20 19
index 0 1 2 3 4
A2 19 20 25 123 412
Index 0 1 2 3 4
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 15
Linear searching in ordered array
A 412 123 25 20 19
index 0 1 2 3 4
A2 19 20 25 123 412
Index 0 1 2 3 4
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 16
Binary searching technique
46
Smaller Larger
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 17
Binary searching technique
18
Algorithm
Binary search
Data structures and algorithms in Java – p.57
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 19
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 20
Question?
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 21
Advantage of ordered arrays
+→ Useful when
+Searches are frequent
+Insertions and deletions are not
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 22
Logarithm
Range Comparisons needed
+Binary search
10 4
→ Log2(N) 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
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 23
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
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 24
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
+The Person Class
+The classDataArray.java Program
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 25
Big O notation
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 26
Algorithms
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 27
Analysis of Algorithms
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 28
What is a computational complexity?
+The same problem can be solved with various algorithms that differ in
efficiencies.
+The computational complexity (or simply speaking, complexity) of an algorithm
is a measure of how “complex” the algorithm is.
+ How difficult is to compute something that we know to be computable?
+ What resources (time, space, machines, ...) will it take to get a result?
+We also often talk instead about how “efficient” the algorithm is
+Measuring efficiency (or complexity) allows us to compare one algorithm to
another
+Here we’ll focus on one complexity measure: the computation time of an
algorithm.
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 29
Running time
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 30
Time complexity of an algorithm
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 31
Time complexity of an algorithm
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 32
Big-Oh Notation
+Another example:
+The function n2 is not O(n)
n2 ≤ cn
n≤c
➔ Cannot find a constant c and n0 to satisfy this equation
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 34
O(1) – constant
+The time needed by the algorithm does not depend on the number
of items
+Example
+Insertion in an unordered array
+Any others ?
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 35
O(N) – Proportional to N
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 36
O(log N) - Proportional to log(N)
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 37
Some common growth orders of functions
constant O(1)
logarithmic O(logn)
linear O(n)
nlogn O(nlogn)
quadratic O(n2)
polynomial O(nb)
exponential O(bn)
factorial O(n!)
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 38
Summary
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 39
Further reading
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 40
Vietnam National University of HCMC
International University
School of Computer Science and Engineering
THANK YOU