Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

UNIT1 - Programming

Week [14] – Efficiency Analysis of Algorithms

1
Lesson Learning Outcome
Pass Merit Distinction

LO1 Define basic algorithms to carry out an operation and


outline the process of programming an application

P1 Provide a definition of M1 Determine the step taken D1 Examine the


what an algorithm is and from writing code to implementation of an
outline the process in execution. algorithm in a suitable
building an application. language. Evaluate the
relationship between the
written algorithm and the
code variant.

2
Algorithm Efficiency

▪ When developing applications it is important to make them


more efficient in order to get the required performance
without wasting resources. Algorithm efficiency become
important due to this.
▪ Specially when handling large volumes of data if your
algorithms are not efficient we will see a notable delays even
with the fastest computers.

3
Execution Time vs
Algorithm Efficiency

▪ One might think we can measure algorithm efficiency by looking at the


execution time of a program on a particular computer.
▪ But this execution time is affected by numerous other factors such as:
▪ CPU speed
▪ Operating System efficiency
▪ Memory Capacity
▪ CPU and Memory load at the time of running

4
Running Time(t) Volume of Data(n)

▪ Therefore, execution time doesn’t really reflect pure efficiency of


the algorithm.
▪ But we know that running time depends on volume of data.
▪ Due to this reason instead of measuring the exact running time
(execution time) we try to establish relationship between running
time and volume data.
▪ We use this principle in Big-O notation. Algorithms are divided into
efficiency classes based on this notation.

5
Big-O Notation

6
Best, Worst and Average Case

▪ Best Case – Here we look at the most favourable situation and check the
efficiency of the algorithm. For example, Sorting a nearly sorted array.
▪ Worst Case – Here we look at most adverse situation and analyse the efficiency of
the algorithm. For example, Sorting an array arranged in descending order to
ascending order.
▪ Average Case – This could the average of the best case and the worst case
scenarios
Generally, when comparing the efficiencies of algorithms, we use either the Worst
Case analysis or Average Case analysis because Best Case may not really show big
difference between the good one and the bad one.

7
Linear Search

▪ Search an array to look for an item by starting to compare the elements


from the 1st one until it is found or until the end of the array is reached. Can
be used on sorted or unsorted arrays as we are comparing each element.

▪ Example – Searching for Item 17 in the following array (Arrow indicates the
current element being compared).

23 45 28 17 29 13 50 80 65 11
0 1 2 3 4 5 6 7 8 9

Start Stop

8
Linear Search - Efficiency

Best Case 1 comparison Item found the 1st element

Item found on the last element


Worst Case n comparisons or searching for non-existing
item

Average Case (n + 1)/2 comparisons Average of the above 2

Big-O Notation = O(n)

9
Binary Search

▪ We compare the middle element and continue the search on upper or lower
half or the array based on the value is greater or smaller than the middle.
This continues until the value is found at the middle or until we confirm the
value is not present.
▪ Example – Searching for Item 29 in the following array

Low High

11 13 17 23 28 29 45 50 65 80
0 1 2 3 4 5 6 7 8 9

Mid

10
Binary Search
Mid High
Low

11 13 17 23 28 29 45 50 65 80
0 1 2 3 4 5 6 7 8 9

Iteration Low High Mid Found


Searching 1 0 9 4 No
2 5 9 7 No
for 29
3 5 6 5 Yes

Iteration Low High Mid Found


Searching 1 0 9 4 No
2 0 3 1 No
for 15 3 2 3 2 No
4 2 1 No / Stop

11
Binary Search - Efficiency

Best Case 1 comparison Item found the 1st element

searching for non-existing


Worst Case Log2 n comparisons
item

(Log2 n + 1)/2
Average Case Average of the above 2
comparisons

Big-O Notation = O( Log2 n )

12
Bubble Sort
Initially 9 5 12 7 2 Iteration 1 59 95 12
7 27
12 2
12
0 1 2 3 4
0 1 2 3 4

▪ N – 1 Passes(Iterations)
Iteration 2 5 7
9 27
9 92 12
▪ Target starts from Last
0 1 2 3 4
Element

▪ Target comes down to 1 Iteration 3 5 27 72 9 12


▪ Adjoining pairs are 0 1 2 3 4

compared and swapped


if they are out of order Iteration 4 2
5 52 7 9 12
0 1 2 3 4

13
Bubble Sort - Efficiency

1 pass
Already sorted array is given
Best Case n-1 comparisons
for sorting
0 interchanges
n-1 pass
Descending order array is
Worst Case n (n-1)/2 comparisons
given for ascending sort
n (n-1)/2 interchanges

n (n-1)/2 comparisons
Average Case Average of the above 2
n (n-1)/2 interchanges

Big-O Notation = O ( n2 )

14
Lesson Summary

▪ Algorithm Efficiency
▪ Execution time vs Algorithm Efficiency
▪ Running time(t) vs Volume of data(n)
▪ Big-O Notation
▪ Best, Worst and Average Case Analysis
▪ Linear Search Efficiency
▪ Binary Search Efficiency
▪ Bubble Sort Efficiency

15

You might also like