Lec3 Algorithm Design24-25 Course2 Stage 2
Lec3 Algorithm Design24-25 Course2 Stage 2
Nadia Fadhil
2- if-then-else statements
If (condition)
{Sequence of statements}
Else
{Sequence of statements}
Here, either sequence 1 will execute, or sequence 2 will execute. Therefore, the worst-case
time is the slowest of the two possibilities:
max (time (sequence 1), time (sequence 2)).
For example, if sequence 1 is O(N) and sequence 2 is O(1) the worst-case time for the whole
if-then-else statement would be O(N).
3- For loops
For (i = 0; i < N; i++)
{Sequence of statements (take O(1)) }
The loop executes N times, so the sequence of statements also executes N times.
11
Lecture 3 Algorithm Design and Analysis 2024-2025 Ass.Prof. Dr. Nadia Fadhil
We assume the statements are O(1), the total time for the for loop is N * O(1), which is O(N)
overall.
4- Nested loops
Loops where the number of iterations of the inner loop is independent of the value of the
outer loop's index.
For example:
For (i = 0; i < N; i++) {
For (j = 0; j < M; j++)
{Sequence of statements}}
The outer loop executes N times. Every time the outer loop executes, the inner loop executes
M times. As a result, the statements in the inner loop execute a total of N * M times.Thus,
the complexity is O(N * M).
In a special case where the stopping condition of the inner loop is j < N instead of j < M (i.e.,
the inner loop also executes N times).
For example, let's consider nested loops where the number of iterations of the inner loop
depends on the value of the outer loop's index.
For (i = 0; i < N; i++)
{
For (j = i+1; j < N; j++)
{Sequence of statements}
}
So the total complexity for the two loops is O(N2).
12
Lecture 3 Algorithm Design and Analysis 2024-2025 Ass.Prof. Dr. Nadia Fadhil
Question in time complexity, which is fastest the time required using recursion or time
required to execute for loop?
The answer:
The time required to execute a for loop is faster than the time required using recursion.
Recursion involves the function calling itself repeatedly until it reaches a base case, which can
result in a large number of function calls and stack frames. This can lead to a higher time
complexity, as well as potential issues with stack overflow and memory usage.
On the other hand, a for loop is a simple control structure that iterates over a range of values
and performs a set of operations. It typically involves fewer function calls and stack frames,
which can result in a lower time complexity and more efficient use of resources.
So the actual time complexity of a program depends on many factors, such as:
1. The specific algorithm.
2. The size of the input data.
3. Hardware and software environment in which the program is executed.
3.3 Common order-of-growth classifications
13
Lecture 3 Algorithm Design and Analysis 2024-2025 Ass.Prof. Dr. Nadia Fadhil
14
Lecture 3 Algorithm Design and Analysis 2024-2025 Ass.Prof. Dr. Nadia Fadhil
In Java, the size of an array is determined by the number of elements in the array and the size
of each element. For a char array, each element is a 2-byte Unicode character. Therefore, the
size of a char array in Java can be calculated using the following formula:
size of char array = 2 * N + 24 bytes where N is the number of elements in the array.
The extra 24 bytes are for the array header, which includes information about the array such as
its length, starting memory address and type.
The header is necessary to allow Java to dynamically allocate and deallocate arrays.
For example, if we have a char array with 10 elements, the size of the array in bytes would be:
15
Lecture 3 Algorithm Design and Analysis 2024-2025 Ass.Prof. Dr. Nadia Fadhil
Padding (uses a multiple of 4 bytes): need to make sure that the total memory usage is aligned
to a multiple of 4 bytes.
For example, if the sum of the overhead and field sizes is 28 bytes, the padding needed would
be 0 bytes because 28 is already a multiple of 4. If it were 29 bytes, then the padding would be
3 bytes to make it 32 bytes (a multiple of 4).
Each object.
So the total memory usage for a data type value:
�Primitive type: 4 bytes for int, 8 bytes for double, …
�Object reference: 8 bytes.
�Array: 24 bytes + memory for each array entry.
�Object: 16 bytes + memory for each instance variable.
�Padding: round up to multiple of 4 bytes.
16
Lecture 3 Algorithm Design and Analysis 2024-2025 Ass.Prof. Dr. Nadia Fadhil
Example3: of the typical memory usage for an object with three instance variables:
public class Person {
private String name;
private int age;
private boolean isStudent;
// constructor and methods omitted
}
Assuming the JVM is using 32-bit references, the memory usage of an instance of this object
would be approximately:
17