Introduction to Programming
Prepared By: Ryan Jay A. Gino-gino
Algorithms
• An algorithm is a step-by-step procedure or formula for
solving a problem.
• It is a finite sequence of well-defined instructions,
typically used to perform a specific task or to solve a
particular class of problems.
• Algorithms are fundamental to computer science and are
used in various applications, from simple calculations to
complex data processing and artificial intelligence.
Key Characteristics of Algorithms
• Key Characteristics of Algorithms:
• Finite: An algorithm must have a clear starting and ending
point.
• Definite: Each step in the algorithm must be precisely
defined.
• Input: An algorithm can have zero or more inputs.
• Output: An algorithm must produce at least one output.
• Effective: The steps of the algorithm must be basic enough
to be carried out, in principle, by a person using only paper
and pencil.
Examples of Algorithms:
• Sorting Algorithms: Procedures for arranging elements in a list in a specific order
(e.g., bubble sort, quicksort, merge sort).
• Search Algorithms: Techniques for finding an element within a data structure (e.g.,
binary search, linear search).
• Mathematical Algorithms: Methods for performing mathematical computations (e.g.,
the Euclidean algorithm for finding the greatest common divisor).
• Machine Learning Algorithms: Procedures used in artificial intelligence to learn from
data (e.g., decision trees, neural networks).
Importance of Algorithms
• Algorithms are crucial in computing as they enable efficient
problem-solving and data processing. They help in optimizing
tasks, ensuring accuracy, and automating repetitive processes,
making them integral to software development, data analysis,
and many other fields.
Algorithm for Making a Cup of Tea:
1. Start
2. Fill Kettle with Water
3. Boil Water
4. Prepare Cup
5. Pour Boiled Water
6. Brew Tea
7. Remove Tea Bag
8. Add Condiments (Optional)
Algorithm for Making a Cup of Tea:
9. Serve and Enjoy
[Link]
Breakdown of Algorithms
• Breakdown of the Algorithm:
• Finite: The process has a clear start (Step 1) and end (Step
10).
• Definite: Each step is clearly defined.
• Input: The inputs include water, a kettle, a cup, a tea bag,
and optional condiments.
• Output: The output is a cup of tea.
• Effective: Each step can be easily carried out.
Algorithm for Getting Ready for Work:
1. Start
2. Wake Up (Turn off the alarm, Get out of bed)
3. Personal Hygiene
4. Get Dressed
5. Have Breakfast
6. Pack Work Essentials
7. Leave the House
8. Commute to Work
Algorithm for Getting Ready for Work:
9. Arrive at Work
[Link] Work
[Link]
Breakdown of the Algorithm:
• Finite: The process has a clear start (Step 1) and end (Step 11).
• Definite: Each step is clearly defined.
• Input: The inputs include toiletries, clothes, food for breakfast,
work essentials, and means of transportation.
• Output: The output is being ready and arriving at work.
• Effective: Each step can be easily carried out.
Pseudocode
• Pseudocode is a method used to describe an algorithm in
a way that is easy to understand.
• It uses plain language and resembles a programming
language, but it is not meant to be executed on a
computer.
• Instead, pseudocode helps programmers and developers
plan and visualize the logic of an algorithm before
implementing it in an actual programming language.
Characteristics of Pseudocode:
• Simplicity: Written in a way that is easy for humans to read and
understand.
• Language-Agnostic: Not tied to any specific programming
language.
• Structure: Follows a structured format, often using common
programming constructs such as loops, conditionals, and
function calls.
• Abstraction: Focuses on the logic and steps of the algorithm
without getting bogged down by syntax details.
Pseudocode Adding Numbers
• START
• INPUT number1
• INPUT number2
• SET sum TO number1 + number2
• OUTPUT sum
• END
Explanation
• START/END: Indicates the beginning and end of the algorithm.
• INPUT number1: Prompts the user to input the first number.
• INPUT number2: Prompts the user to input the second number.
• SET sum TO number1 + number2: Adds the two numbers and
stores the result in the variable sum
• OUTPUT sum: Displays the value of sum.
Finding Highest Number
• START
• INPUT number1
• INPUT number2
• IF number1 > number2 THEN
• OUTPUT "The highest number is " + number1
• ELSE
• OUTPUT "The highest number is " + number2
• END IF
• END
Explanation
• START/END: Indicates the beginning and end of the algorithm.
• INPUT number1: Prompts the user to input the first number.
• INPUT number2: Prompts the user to input the second number.
• IF number1 > number2 THEN: Checks if the first number is greater than the
second number.
• OUTPUT "The highest number is " + number1: If the condition is true, it
displays that the highest number is number1.
• ELSE: If the condition is not true (meaning number2 is greater than or equal
to number1).
• OUTPUT "The highest number is " + number2: Displays that the highest
number is number2.
• END IF: Ends the conditional statement.