An algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem, based on conducting a sequence of specified actions.
Brute Force: Generate every possible combination of solution.
Divide and conquer: Logarithmic runtime – Divide the size of problem by a constant.
Decrease and conquer: Similar Divide Conquer -- decrease the problem in each iteration.
Dynamic programming: Exchange the space with time as when it re-computes it holds precomputed values for solved problem.
Greedy algorithm: Work by recursively, starts from smallest part.
Transform and conquer: Transform a problem into another like finding GCD to actually find LCM.
Backtracking algorithm: We only use the solution to ensure a 100% success rate.
2. Algorithm
• An algorithm (pronounced AL-go-rith-um) is a procedure or formula
for solving a problem, based on conducting a sequence of specified
actions.
• An algorithm has a statement which are lead from an input of a
problem to an output as a solution
• The word algorithm derives from the name of the mathematician,
Mohammed ibn-Musa al-Khwarizmi, who was part of the royal court
in Baghdad and who lived from about 780 to 850.
6. BigO Notation Table
f(n) Name
1 Constant
Log n Logarithmic
N Linear
N log n Log Linear
N2 Quadratic
N3 Cubic
2n Exponential
7. BigO
• BigO will provide an explanation about the procedure which has been or which
will be required to complete a certain task.
• BigO is a notation used for programmer to analyses an Algorithm. The statement
which is used to describe BigO is as follow.
8. Brute Force
• It is a programming technique which means trying all the probabilities
which an algorithm may offer.
• An example is a PADLOCK which has a set of numbers to unlock the
chain, if a user doesn’t know the right code it has to loop all the
probabilities.
SWITCH
Case 1:
Case 2:
Break
switch(expression)
{ case value : //
Statements break; //
optional case value : //
Statements break; //
optional // You can have any number of case statements.
default : //
Optional //
Statements }
10. Binary Search BigO(log n)
Divide and conquer
Middle = List[]/2
Left=Middle
Right = L-length.ist
5 44 17 22 2 18 54 84
5 44 17 22 2 18 54 84
0 1 2 3 4 5 6 7
2 175 18 22 44 54 84Merge Sort Return.
Index [] list
Sort starts
from left.
int[] list;
END
L.Left
element >
R.Right
TRUE
swap
Ascending
Order int[] list;
FALSE
Left[] || Right[]; Return;
Else
IF
Search
# 22
Transform and conquer
List[]/2
Left[] Right[]
Found
#
Return
index
2 175 18 22 44 54 84
Lower-bond upper-bond
22
Index [] list
0 1 2 3 4 5 6 7
Found at 5th position
11. Test Your Self
What is the worst-case complexity of the each of the following code fragments?
1.Two loops in a row:
for (i = 0; i < N; i++) { sequence of statements } // loop n
for (j = 0; j < M; j++) { sequence of statements } // loop m
How would the complexity change if the second loop went to N instead of M?
2.A nested loop followed by a non-nested loop:
for (i = 0; i < N; i++) // loop n
{
for (j = 0; j < N; j++) { sequence of statements } // loop n (NESTED LOOP)
}
for (k = 0; k < N; k++) { sequence of statements } // loop m
BigO(n+m)
BigO(n^2+m)