Professional Documents
Culture Documents
Algorithms Section1 1
Algorithms Section1 1
Algorithms Section1 1
Section Content:
Introduction
Algorithms:
Sequence of steps that can be taken to solve a problem.
OR
A process or a set of rules to be followed in calculations or other problem solving
operations. (especially by a computer).
Sequence: line by line
Iterative (Repetitive): loops (for, do while, while)
Recursive: loops by calling method it self
Decision: (if-else)
When faced with a problem:
1. We first clearly define the problem
2. Think of possible solutions
3. Select the one that we think is the best under the prevailing circumstances
4. And then apply that solution
5. If the solution woks as desired, fine; else we go back to step 2
Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
Write an algorithm to find the largest of a set of numbers. You do not
know their exact number?!
Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
Write an algorithm to find the largest of a set of numbers. You do not
know their exact number?!
Input: a list of positive integer
1. Set largest to zero
2. While (more integers)
2.1. If (the integer is greater than largest)
then
2.1.1. Set the value of integer to the largest
End if
End while
3. Return largest
End
Write algorithm to find the largest number from a list of positive integer size (1000)?!
Input: 1000 positive integer
5. Set largest to zero
6. Set counter to zero
7. While (counter less than 1000)
7.1. If (the integer is greater than largest)
then
7.1.1. Set the value of integer to the largest
End if
7.2. Increment counter
End while
8. Return largest
End
Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
To design an algorithm there are two basic issues should be considered:
Efficiency: a problem can be solved by several algorithms different from the
other in its efficiency.
Function of:
Space (storage)
Time of execution (solving problem)
Correctness: ensuring that the algorithm produces the correct output for all
possible input.
The correctness of the algorithm is achieved through:
Mathematical proof
Induction
Contradiction
Direct proof
Indirect proof
Exhaustion
Testing
Mathematical rules:
∑ symbol: indicates summation.
Π symbol: indicates product
𝑡
Mod operators: the result of a mod division is the remainder of an integer division
of the given number.
30 mod 3=0
25 mod 5=0
3 mod 25=3
2 mod 10=2
27 mod 16=11
35 mod 3=2
(a+b)2 = a2+2ab+b2
(a-b)2 = a2-2ab+b2
The cube brackets formulas:
Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed