J.E.D.I
e.g.
3.14
= 4
12
= 1
-12
= 0
• Modulo - Given any two real numbers x and y, x mod y is defined as
x mod y = x
if y = 0 = x - y
x y
if y 0
e.g. 10 mod 3 = 1 24 mod 8 = 0
-5 mod 7 = 2
Identities
The following are the identities related to the mathematical functions defined above: •
x
=
x
if and only if x is an integer
•
x
=
x
if and only if x is not an integer •
- x
= -
x
•
x
+
y
=
x + y
• x =
x
+ x mod 1
• z x mod y = zx mod zy
1.8 Complexity of Algorithms
Several algorithms could be created to solve a single problem. These algorithms may vary in the way they get, process and output data. Hence, they could have significant
difference in terms of performance and space utilization. It is important to know how to analyze the algorithms, and knowing how to measure the efficiency of algorithms helps a
lot in the analysis process.
1.8.1 Algorithm Efficiency
Algorithm efficiency is measured in two criteria: space utilization and time efficiency. Space utilization is the amount of memory required to store the data while time
efficiency is the amount of time required to process the data.
Before we can measure the time efficiency of an algorithm we have to get the execution time. Execution time is the amount of time spent in executing instructions of a given
algorithm. It is dependent on the particular computer hardware being used. To express the execution time we use the notation:
Tn, where T is the function and n is the size of the input
There are several factors that affect the execution time. These are: •
input size •
instruction type •
machine speed •
quality of source code of the algorithm implementation •
quality of the machine code generated from the source code by the compiler
Data Structures 15
J.E.D.I
The Big-Oh Notation
Although Tn gives the actual amount of time in the execution of an algorithm, it is easier to classify complexities of algorithm using a more general notation, the Big-Oh or
simply O notation. Tn grows at a rate proportional to n and thus Tn is said to have “order of magnitude n” denoted by the O-notation:
Tn = On
This notation is used to describe the time or space complexity of an algorithm. It gives an approximate measure of the computing time of an algorithm for large number of
input. Formally, O-notation is defined as:
gn = Ofn if there exists two constants c and n such that
| gn | = c | fn | for all n = n .
The following are examples of computing times in algorithm analysis:
Big-Oh Description
Algorithm
O1 Constant
Olog
2
n Logarithmic
Binary Search On
Linear Sequential Search
On log
2
n Heapsort
On
2
Quadratic Insertion Sort
On
3
Cubic Floyd’s Algorithm
O 2
n
Exponential To make the difference clearer, lets compare based on the execution time where
n=100000 and time unit = 1 msec:
Fn Running Time
log
2
n 19.93 microseconds
n 1.00 seconds
n log
2
n 19.93 seconds
n
2
11.57 days n
3
317.10 centuries 2
n
Eternity
1.8.2 Operations on the O-Notation