Method of solving computing problems (introduction to algorithm)
Subject: Problem Solving (COS 102)
Methods of Solving Computing Problems
1.
Problem Definition: Clearly state the problem (e.g., "Find the largest number in a list").
2.
Algorithm Design: Use pseudocode or flowcharts to outline steps.
Example pseudocode for finding the largest number:
Code
Start
Set max = first element
For each element in list:
If element > max:
Set max = element
End
3.
Implementation: It is at this point you write code using a programming language such as Python to execute what you have in your pseudocode or flowcharts
4.
Testing and Debugging: run with different inputs to ensure correctness.
What is Algorithm
An algorithm is a precise, step-by-step procedure designed to solve a specific problem or perform a computation. It acts like a recipe in cooking: just as a recipe provides clear instructions to prepare a dish, an algorithm provides clear instructions to achieve a computational goal.
Algorithms are fundamental in computer science because they ensure that problems are solved systematically, efficiently, and consistently. They must have well-defined inputs, produce expected outputs, and terminate after a finite number of steps.
For example, consider the problem of finding the largest number in a list. The algorithm would start by assuming the first number is the largest, then compare it with each subsequent number, updating the "largest" whenever a bigger number is found.
numbers = [3, 7, 2, 9, 5]
# assume first element is the largest
max_num = numbers[0]
# loop through the list
for num in numbers:
if num > max_num:
max_num = num
print(max_num)
Explanationa. Initialization: Start by assuming the first element is the largest.
b. Iteration: Go through each number in the list.
c. Comparison: If a number is greater than the current maximum, update it.
d. Output: Print the largest number after the loop ends.
Properties of an Algorithm
1.
Clear and Unambiguous: every step must be precise and have only one interpretation. Example: If an algorithm says "process the data," it is vague. Instead, it should specify "sort the list in ascending order."
2.
Well-Defined Inputs: An algorithm may take zero or more inputs, but they must be clearly specified. Example: A sorting algorithm requires a list of numbers as input.
3.
Well-Defined Outputs: It must produce at least one output that is clearly defined. Example: A search algorithm outputs either the position of the found element or "not found."
4.
Finiteness: The algorithm must terminate after a finite number of steps. Infinite loops or recursion without a base case violate this property.
5.
Effectiveness: Each step should be simple, feasible, and executable with available resources. Example: "Add two numbers" is effective, but "solve world hunger" is not.
6.
Deterministic: For the same input, the algorithm should always produce the same output.
7.
Language Independent: Algorithms are plain instructions and can be implemented in any programming language. Example: The logic of bubble sort remains the same whether written in Python, Java, or C++.
By:
Vision University
Login to comment or ask question on this topic
Previous Topic