0% found this document useful (0 votes)
29 views11 pages

Big O Notation P3

This document discusses Big-O notation and how it is used to express the time complexity of algorithms. It provides examples of common time complexities such as O(1), O(N), and O(N^2) and explains how to determine the time complexity of different code snippets.

Uploaded by

Irsyad Andre
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)
29 views11 pages

Big O Notation P3

This document discusses Big-O notation and how it is used to express the time complexity of algorithms. It provides examples of common time complexities such as O(1), O(N), and O(N^2) and explains how to determine the time complexity of different code snippets.

Uploaded by

Irsyad Andre
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/ 11

BIG-O NOTATION

BIG-O NOTATION
THIS EXPRESSES THE COMPLEXITY OF AN ALGORITHM

AN ALGORITHM WHOSE
COMPLEXITY DOES NOT CHANGE
WITH THE INPUT SIZE IS O(1)

THE ALGORITHM IS SAID TO


HAVE CONSTANT TIME
COMPLEXITY

IT TAKES THE SAME AMOUNT OF TIME EVEN IF THE


INPUT SIZE IS DOUBLED, TRIPLED OR INCREASED TO ANY
LEVEL
IF “N” IS THE SIZE OF THE INPUT….

THE COMPLEXITY OF AN
ALGORITHM IS O(N) IF THE
TIME TAKEN BY THE
ALGORITHM INCREASES
LINEARLY WHEN N
INCREASES

THE COMPLEXITY OF AN
ALGORITHM IS O(N2) IF THE
TIME TAKEN BY THE
ALGORITHM INCREASES
QUADRATICALLY WHEN N
INCREASES
WHAT IS THE COMPLEXITY OF COMMON
OPERATIONS?

THE COMPLEXITY OF AN THE COMPLEXITY OF AN


ALGORITHM IS O(N) IF THE ALGORITHM IS O(N ) IF
2

TIME TAKEN BY THE THE TIME TAKEN BY THE


ALGORITHM INCREASES ALGORITHM INCREASES
LINEARLY WHEN N QUADRATICALLY WHEN N
INCREASES INCREASES

LOWER ORDER TERMS AND


CONSTANTS DO NOT MATTER WHILE
EXPRESSING COMPLEXITY, THE
ASSUMPTION IS THAT N IS VERY
LARGE

O(N 2 + 1000) IS EQUIVALENT TO O(N 2 )


O(N2 + N) IS EQUIVALENT TO O(N2)
WHICH ALGORITHMS ARE FASTER?

TIME TAKEN
O(1) < O(N) < O(N2) < O(N3)
FASTEST SLOWEST
WHAT ARE THE COMPLEXITIES OF
THE FOLLOWING PIECES OF CODE?
HINT: NOTE THAT WE DON’T CARE
ABOUT THE ACTUAL NUMBER
OF OPERATIONS, COMPLEXITY
IS BASED ON THE SIZE OF THE INPUT

THIS IS A CONSTANT TIME


OPERATION, O(1). THE CODE TAKES
THE SAME TIME WHATEVER THE VALUE
OF N, IT USES THE VALUE OF N, RATHER
THAN USE IT AS A SIZE OF INPUT
HINT: THE NUMBER OF OPERATIONS
OBVIOUSLY CHANGES WITH THE SIZE OF
THE INPUT
THE COMPLEXITY OF THIS
OPERATION IS O(N), THE
OPERATIONS CHANGE
LINEARLY WITH THE SIZE
OF INPUT

IF THE VALUE OF “N” DOUBLES, THE CODE WILL TAKE


ROUGHLY TWICE AS LONG
HINT: THIS IS CONCEPTUALLY SIMILAR TO
THE PREVIOUS ONE
THE COMPLEXITY OF THIS
OPERATION IS O(N), THE
OPERATIONS CHANGE
LINEARLY WITH THE SIZE OF
INPUT

IF THE VALUE OF “N” DOUBLES, THE CODE WILL TAKE


ROUGHLY TWICE AS LONG
HINT: COMPLEXITY ANALYSIS IS BASED ON
THE WORST CASE SCENARIO
THE COMPLEXITY OF THIS
OPERATION IS O(N), THE
WORST CASE IS THAT THE
INPUT IS ODD AND WE ENTER
THE FOR-LOOP

IN THE FOR LOOP IF THE VALUE OF “N” DOUBLES,


THE CODE WILL TAKE ROUGHLY TWICE AS LONG
HINT: A SINGLE LOOP OF N IS O(N), TWO NESTED LOOPS
WILL BE OF HIGHER COMPLEXITY

THE COMPLEXITY OF THIS


OPERATION IS O(N2), AS N
CHANGES THE NUMBER
OF OPERATIONS CHANGE
QUADRATICALLY

FOR EVER ITERATION OF THE OUTER LOOP THE INNER


LOOP ITERATES N TIMES SO THE STATEMENT IS CALL
N*N TIMES = N2

You might also like