Alg Ch1 Part1
Alg Ch1 Part1
Alg Ch1 Part1
Thaer Thaher
Thaer.Thaher@aaup.edu
Fall 2023/2024
Chapter1: Introduction
Prerequisites
The course assumes that a student has gone through
An introductory programming course.
Fundamental data structures.
Mathematics Background (summation formulas , recurrence relations …)
Some of the popular programming languages include Python, Java, C/C++, C#,
Visual Basic, JavaScript, PHP, and Ruby.
Software development process
Stages of program development:
1. Specify the problem requirements: Understand what the software should do.
Start
End
Characteristics of Algorithm
Algorithms have the following main properties: Input Algorithm Output
Input
It should take valid and well-defined inputs (uses values from a specified list)
Output:
It should produce the correct output given a valid input.
Precision / definiteness:
the steps are precisely stated (should be unambiguous).
Finiteness:
the algorithm terminates after a finite number of steps.
Effectiveness:
steps are sufficiently simple and basic. You should not write unnecessary statements
Characteristics of Algorithm
Other properties
Determinism:
the intermediate results of each step of execution are
unique and
• are determined only by the inputs and results of the preceding steps
Correctness:
the output is correct for each input as defined by the problem.
Generality:
the algorithm should be applicable a set of inputs (not a special subset).
It should be language-independent.
Write the line of codes independent of all language specific words, terms or notation.
Example
Example:
Problem: Finding the max of 3 numbers (a, b, c)
Algorithm:
1. x=a
2. If b>x, then x=b
3. If c>x, then x=c
ALGORITHM Euclid ( m , n)
{
WHILE n ≠ 0 do
r ← m mod n
m← n
n←r
ENDWHILE
RETURN m
}
13
Other methods for computing gcd(m,n)
14
Other methods for gcd(m,n) [cont.]
Middle-school procedure
Note: Every integer greater than 1 is
• Step 1 Find the prime factorization of m either a prime number or can be
written as a product of its prime factors
• Step 2 Find the prime factorization of n
Main problem
Sorting Insertion sort, selection sort, bubble sort, heap sort, quick sort, merge sort
…….
16
Differences Between Algorithm and Program
Algorithm Program
Recall software
development life cycle
Design phase Implementation phase
** An algorithm is the thing which stays the same whether the program is
Java or C++…etc.
The Problem-solving Process
Analysis
Problem
specification
Design
Algorithm
Implementation
Program
Compilation
Executable
(solution)
Two main aspects in the study of algorithms
Analyzing algorithms
o Theoretical Analysis
o Empirical Analysis
These techniques provide a student with tools for designing algorithms for new
design idea.
Analysis of algorithms
In the analysis of algorithms, we ask the following questions:
Time efficiency
• how many instructions does the algorithm execute?
Space efficiency
• how much memory does the algorithm need to execute?
In this course, we are concerned primarily with the time analysis. Why?
Analysis of algorithms [cont.]
In the analysis of algorithms, we ask the following questions:
Ease up code construction: It’s one of the best approaches to start implementation of
an algorithm.
Max(a, b, c)
Begin
x ←a
IF (b>x) Then
• BEGIN … END can be replaced with { }
x←b curly braces
IF (c>x) Then
x←c • Different symbols can be used for assignment
End
RETURN x
operator (= , := , ←)
Example: Finding the max value in an array
Function Sum_odd_1_to_n ( n )
{ As pseudocode does not follow a strict
sum = 0
systematic or standard way of being written,
so don’t think of writing pseudocode as a
FOR count = 1 to n step 2
strict rule
add count to sum
1. Write a pseudocode that will calculate a running sum. A user will enter numbers that will be added
to the sum and when a negative number is encountered, stop adding numbers and write out the
final result.
5. Write a pseudocode to find the sum and average of even numbers in a given array B
Control structures.
Functions
Recursive functions
}
Create a function (Function Declaration)
To declare a function, we need to specify: