Algorithms Section1 1

You might also like

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

Algorithms Section(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?!

Input: 5 positive integer


1. Set largest to zero
2. Set counter to zero
3. While (counter less than 5)
3.1. If (the integer is greater than largest)
then
3.1.1. Set the value of integer to the largest
End if
3.2. Increment counter
End while
4. Return largest
End

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
𝑡

∑ 𝑓(𝑖) = 𝑓(𝑠) + 𝑓(𝑠 + 1) + ⋯ + 𝑓(𝑡)


𝑖=𝑠
f: is the function
s: is the start index
t: is the end index

Logarithms & exponent:


log 𝑏 𝑋𝑌 = log 𝑏 𝑋 + log 𝑏 𝑌
𝑋
log 𝑏 = log 𝑏 𝑋 − log 𝑏 𝑌
𝑌
log 𝑏 𝑋 𝛼 = 𝛼 log 𝑏 𝑋
log 𝑎 𝑎 = 1
log 𝑎 𝑎𝑏 = 𝑏 log 𝑎 𝑎 = 𝑏
𝑐
𝑎𝑐 log𝑎 𝑏 = 𝑎log𝑎 𝑏 = 𝑏 𝑐
𝑎(𝑏+𝑐) = 𝑎𝑏 . 𝑎𝑐
𝑎(𝑏−𝑐) = 𝑎𝑏 /𝑎𝑐
𝑎𝑏𝑐 = (𝑎𝑏 )𝑐
𝑚 𝑛
𝑥𝑛 = √𝑥 𝑚
Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed
Floor & ceiling operators:
┌┐
(0) = 0 =└ 0 ┘
┌┐
(1.01) = 2 =└ 1 ┘
┌┐
(2.99) = 3 =└ 2 ┘
┌ ┐
(-1.1) = -1 =└-2┘

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

The square brackets formulas:

 (a+b)2 = a2+2ab+b2
 (a-b)2 = a2-2ab+b2
The cube brackets formulas:

 (a+b)3 = a3+3ab(a+b) +b3


 (a-b)3= a3-3ab(a-b) -b3

Eng. Rania Ahmed Eng. Sohaila Ahmed Eng. Nadeen Qadry Eng. Mostafa Sayed

You might also like