0% found this document useful (0 votes)
10 views41 pages

Lecture 02 - Arrays

The document outlines a course on Data Structures and Algorithms with a focus on Arrays in Java, detailing topics such as initialization, accessing elements, searching techniques (linear and binary), and Big O notation for algorithm efficiency. It emphasizes the importance of understanding array operations and computational complexity. The course includes practical lab sessions and a final exam, along with recommended readings for further study.

Uploaded by

dogiathuyasd18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views41 pages

Lecture 02 - Arrays

The document outlines a course on Data Structures and Algorithms with a focus on Arrays in Java, detailing topics such as initialization, accessing elements, searching techniques (linear and binary), and Big O notation for algorithm efficiency. It emphasizes the importance of understanding array operations and computational complexity. The course includes practical lab sessions and a final exam, along with recommended readings for further study.

Uploaded by

dogiathuyasd18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Vietnam National University of HCMC

International University
School of Computer Science and Engineering

Data Structures and Algorithms


★ Arrays ★

Dr Vi Chi Thanh - [email protected]


https://vichithanh.github.io
Week by week topics (*)

1. OOP and Java 7. Advanced Sorting


2. Arrays 8. Binary Tree
3. Sorting 9. Hash Table
4. Queue, Stack 10.Graphs
5. List 11.Graphs Adv.
6. Recursion Final-Exam
Mid-Term 8 LABS

MO N D A Y , 1 6 S EP T EM BE R 2 0 24 2
Objective

+Basics of Array in Java


+Linear search – Binary search
+Storing objects
+Big O notation

MO N D A Y , 1 6 S EP T EM BE R 2 0 24 3
Introduction

+How do we store list of integer entered by user?

int num1;
int num2;
int num3;

+If there are 100 of them !?

MO N D A Y , 1 6 S EP T EM BE R 2 0 24 4
Introduction

+Array is the most commonly used data structure


+Built into most of the programming languages

123 412 19 20

MO N D A Y , 1 6 S EP T EM BE R 2 0 24 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

MO N D A Y , 1 6 S EP T EM BE R 2 0 24 6
Initialization

+By default, an array of integers is automatically initialized to 0 when


it’s created
autoData[] carArray = new autoData[4000];

+Initialize an array of a primitive type


int[] intArray = { 0, 3, 6, 9, 12, 15, 18, 21, 24, 27 };

MO N D A Y , 1 6 S EP T EM BE R 2 0 24 7
Accessing array elements

+Element is access by index number via subscript


+In Java,
ArrayName[index]
+A[3], A[0]
+A[6]

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};

for (int i=0; i< 100; i++)


A1[i] = i*2;

for (int j=0; j< A2.length; j++)


A2[j] = j*2;
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 9
Operations on array

+Insertion
+Searching
+Deletion

+Duplication issue

→How does it work ?


→Java code in p.41-42

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

+Look for ‘20’ (the SearchKey)

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

+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

MO N D A Y , 1 6 S EP T EM BE R 2 0 24 15
Linear searching in ordered array

+Look for ‘20’

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

+Guess the number between 1 and 100

46
Smaller Larger

MO N D A Y , 1 6 S EP T EM BE R 2 0 24 17
Binary searching technique

Step # Number guessed Result Range of possible value


0 0-100
1 50 Too high 0-49
2 25 Too low 26-49
3 37 Too low 38-49
4 43 Too low 44-49
5 46 CORRECT BINGO !!!

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?

+Modify the Binary search algorithm for a descending array?

MO N D A Y , 1 6 S EP T EM BE R 2 0 24 21
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

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

+To measure the EFFICIENCY of algorithms


+Some notions
+Constant
+Proportional to N
+Proportional to log(N)
+Big O relationships between time and number of items

MO N D A Y , 1 6 S EP T EM BE R 2 0 24 26
Algorithms

+Are sequences of instructions


+To solve a problem
+In a finite amount of time

MO N D A Y , 1 6 S EP T EM BE R 2 0 24 27
Analysis of Algorithms

+What are requirements for a good algorithm?


+Precision:
+Proved by mathematics
+Implementation and test
+Simple
+Effectiveness:
+Run time duration (time complexity)
+Memory space (space complexity)

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

+Most algorithms transform input objects into


output objects
+The running time of an algorithm typically
grows with the input size
+Average case time is often difficult to
determine
+We focus on the worst-case running time

MO N D A Y , 1 6 S EP T EM BE R 2 0 24 30
Time complexity of an algorithm

+Run time duration of a program depend on


+Size of data input
+Computing system (platform: operation system, speed of CPU, type of
statement…)
+Programming languages
+State of data
➔ It is necessary to evaluate the run time of a program such that it
does not depend on computing system and programming languages.

MO N D A Y , 1 6 S EP T EM BE R 2 0 24 31
Time complexity of an algorithm

+Time complexity = the number of operations given an input size.


+ What is meant by “number of operations”?
+ What is meant by “size”?
+The number of operations performed = function of the input size n.
+What if there are many different inputs of size n?
+ Worst case
+ Best case
+ Average case
+“number of operations” = “running time”?

MO N D A Y , 1 6 S EP T EM BE R 2 0 24 32
Big-Oh Notation

+Given function f(n) and g(n), we


say that f(n) is O(g(n)) if there
are positive constants c and n0
such that
f(n) ≤ c*g(n) for all n ≥ n0
+Example: 2n + 10 is O(n)
2n + 10 ≤ cn
(c – 2)n ≥ 10
n ≥ 10(c – 2)
➔ Pick c = 3 and n0 = 10
MO N D A Y , 1 6 S EP T EM BE R 2 0 24 33
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

+Linear search of K items in an array of N items


On average T = K * N /2
+Average linear search times are proportional to size of array.
+For an array of N’ items
If N’ = 2 * N
Then T’ = 2 * T

MO N D A Y , 1 6 S EP T EM BE R 2 0 24 36
O(log N) - Proportional to log(N)

+For binary search:


T = K * log2(N)

+We can say : T = K * log(N)


+any logarithm is related to any other logarithm by a constant (3.322 to go
from base 2 to base 10
+we can add the difference between log2 and log into K

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

+Arrays in Java are objects, created with new operator


+Unordered arrays offer
+ fast insertion
+ 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 bad

MO N D A Y , 1 6 S EP T EM BE R 2 0 24 39
Further reading

+Lafore, R. (2017). Data Structures and Algorithms in Java. United


Kingdom: Pearson Education.
+Chapter 2: Arrays (p.33)

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

Dr Vi Chi Thanh - [email protected]


https://vichithanh.github.io

You might also like