Professional Documents
Culture Documents
FINAL
MO3AZ SAIF
Introduction to algorithms
What is an algorithm ?
Algorithm must be ?
MO3AZ SAIF
Algorithm Design
What is Flowchart ?
A visual way of representing by diagrams that uses shapes, lines, and arrows to sequence steps.
Start & End Process Input & Output Conditions (IF) Directions
What is Pseudocode ?
A description of the algorithm steps using a mixture of programming languages and English statements.
MO3AZ SAIF
Algorithm Analysis
What is algorithm efficiency ?
The amount of time and space resources required to execute it.
BIG-O Notation ?
- A mathematical notation that describes the complexity of your code.
Constant Logarithmic Linear Loglinear Polynomial Exponential Factorial
O( 1 ) O( log(n) ) O( n ) O( n log(n) ) O( 𝑛𝑎 ) O( 𝑎𝑛 ) O( n! )
MO3AZ SAIF
Recursive Function
What is Recursion ?
A function that calls itself directly or indirectly to solve a problem. It contains two cases:
Base Case ( Solution statement ) General Case ( Recursive function call )
) ده مثال الدكتور اتكلم فيه كتير مش القي ليه اي اهمية غير كان عايز يوصل ازاي ممكن نحسن الكود بطرق مختلفة (الجوريزم مختلف MO3AZ SAIF
Money Change Problem [ Assignment 1 ]
Our coins come in denominations of 1 cent, 5 cents, 10 cents, 20 cents, 50 cents and 1 dollar. Assuming that
there are unlimited number of coins, how would you devise a coin-change algorithm to compute the minimum
number of coins required to make up a particular amount? For example, for 46 cents you need 4 coins: two 20-
cent coins, one 5-cent coin, and one 1-cent coin.
START
READ m 100 50 20 10 5 1
MO3AZ SAIF
FLOWCHART &
PSEUDOCODE
MO3AZ SAIF
Q1: Write an algorithm and draw a flowchart to sum two numbers.
Pseudocode Flowchart
START
READ n1,n2 Start
SET Sum n1+n2
PRINT Sum Read n1 , n2
END
Sum = n1 + n2
Print Sum
End
MO3AZ SAIF
Q2: Write an algorithm and draw flowchart to calculate area of rectangle the length L ,
and the width of the rectangle W.
Pseudocode Flowchart
START
READ L , W Start
SET Area L * W
PRINT Area Read L , W
END
Area = L * W
Print Area
End
MO3AZ SAIF
Q3: Write an algorithm and draw a flowchart to find the average of 3 integers.
Pseudocode Flowchart
START
READ n1,n2,n3 Start
Print Avg
End
MO3AZ SAIF
Q4: Write an algorithm and draw a flowchart to find the largest of two numbers.
Pseudocode Flowchart
START
READ a , b Start
IF a > b THEN
PRINT a Read a , b
ELSE
PRINT b YES NO
a>b?
END IF
END
Print a Print b
End
MO3AZ SAIF
Q5: Write an algorithm and draw a flowchart to find the largest of three numbers.
Pseudocode Flowchart
START
Start
READ a , b , c
IF a >= b AND a >= c THEN Read a, b , c
PRINT a
ELSE NO
YES a >= b &&
IF b >= a AND b >= c THEN a >= c ?
b >= a &&
PRINT b b >= c ?
NO
ELSE
Print c YES
PRINT c
Print a Print b
END IF
END IF End
END
MO3AZ SAIF
Q6: Write an algorithm and draw flowchart to print natural numbers from 1 to 100.
Pseudocode Flowchart
START
SET i 1 Start
WHILE i <= 100 DO
PRINT i i=1
SET i i + 1
YES NO
END WHILE i <= 100 ?
END
Print i
i=i+1
End
MO3AZ SAIF
Q7: Write an algorithm and draw flowchart to calculate sum of numbers 0 ---> 100.
Pseudocode Flowchart
START
SET i 0 , Sum 0 Start
i=i+1
End
MO3AZ SAIF
Q8: Write an algorithm and draw a flowchart to print even numbers from 16 to 48.
Pseudocode Flowchart
START
SET i 16
Start
WHILE i <= 48 DO
PRINT i
i = 16
SET i i + 2
END WHILE YES NO
i <= 48 ?
END
Print i
i=i+2
End
MO3AZ SAIF
Q9: Write an algorithm and draw a flowchart to calculate sum of the age of student and
print sum and average.
Pseudocode Flowchart
START Start
READ n
SET i 1 , Sum 0 Read n
WHILE i <= n DO
i = 1 , Sum = 0
READ Age
SET Sum Sum + Age
YES NO
i <= n ?
SET i i + 1
Read Age
END WHILE Avg = Sum / n
SET Avg Sum / n Sum = Sum + Age
Print Sum , Avg
PRINT Sum , Avg
i=i+1
END End
MO3AZ SAIF
Q10: Write an algorithm and draw a flowchart to find factorial of n.
Pseudocode Flowchart
START
Start
READ n
SET i 1 , Fact 1 Read n
WHILE i <= n DO
i = 1 , Fact = 1
SET Fact = Fact * i
SET i i + 1
YES NO
i <= n ?
END WHILE
PRINT Fact Fact = Fact * i
Print Fact
END
i=i+1
End
MO3AZ SAIF
TIME
COMPLEXITY
MO3AZ SAIF
EXAMPLE ( 1 ) :
Public int sum(int n){
int total;
total=n*(n+1)/2 ; 1
Return total; 1
}
F(n) = 1 + 1 = 2
O( F(n) ) = O(1)
MO3AZ SAIF
EXAMPLE ( 2 ) :
Public int sum (int n) {
int i , total;
total = 0; 1
For( i=1 ; i <= n ; i++ ){ (n+1 - 1) + 1 = n + 1
total = total + i ; (n+1) - 1 = n
}
Return total ; 1
}
F(n) = 1 + ( n + 1 ) + n + 1 = 2n + 3
O( F(n) ) = O(n)
MO3AZ SAIF
EXAMPLE ( 3 ) :
X=0; 1
for(int i = -2 ; i < n ; i++){
( n - (-2) ) + 1 = n + 3
x=x+i; (n+3)-1=n+2
y=x+2; ( n + 3) - 1 = n + 2
}
Print(x); 1
MO3AZ SAIF
EXAMPLE ( 4 ) :
Public int sum() {
int i , total;
total = 0 ; 1
for( i = 1 ; i <= 20 ; i++){ ( 20 + 1 - 1) + 1 = 21
total = total + i ; 21 - 1 = 20
}
Return total ; 1
}
F(n) = 1 + 21 + 20 + 1 = 43
O( F(n) ) = O(1)
MO3AZ SAIF
Bubble Sort
The simplest sorting algorithm that works by repeatedly Example :
swapping the adjacent elements if they are in the wrong order.
3 1 4 2
Time Complexity in any case : O( 𝑛2 )
3 1 4 2 3 > 1 - Swap
1 3 4 2 4 > 2 - Swap
function bubbleSort(arr , n ):
for i from 0 to n-1: 1 3 2 4 1 > 3 - No Swap
MO3AZ SAIF
Selection Sort
A simple sorting algorithm that works by repeatedly selecting Example :
1 3 4 2
Pseudocode :
Min = 3 3 > 4 => Min = 3
Min = 3 3 > 2 => Min = 2
function selectionSort( arr , n )
Pass #2 : Min = 2 - Swap( 3 , 2 )
for i from 0 to n - 1
1 2 4 3
minIndex = i Min = 4 4 > 3 => Min = 3
for j from i+1 to n Pass #3 : Min = 3 - Swap( 4 , 3 )
minIndex = j
1 2 3 4
swap(arr[i], arr[minIndex])
MO3AZ SAIF
Insertion Sort
A sorting algorithm that works by considering one element at a Example :
Worst Case : O( 𝑛2 ) , [ array in reversed order ] Shift element ( 3 ) and insert key (1)
into its correct position
Pseudocode :
1 3 4 2
MO3AZ SAIF