0% found this document useful (0 votes)
5 views3 pages

Algorithms

An algorithm is a finite set of clear instructions for performing a specific task or solving a problem. Key characteristics of a good algorithm include finiteness, definiteness, input, output, and feasibility. Algorithms can be represented through pseudocode or flowcharts, and their efficiency is analyzed based on time and space complexity, often using Big O Notation.

Uploaded by

mianmusa160
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Algorithms

An algorithm is a finite set of clear instructions for performing a specific task or solving a problem. Key characteristics of a good algorithm include finiteness, definiteness, input, output, and feasibility. Algorithms can be represented through pseudocode or flowcharts, and their efficiency is analyzed based on time and space complexity, often using Big O Notation.

Uploaded by

mianmusa160
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Since an algorithm is the foundation of all computer science, let's dive into the details of what

makes them tick, how we represent them, and what constitutes a "good" one.

What is an Algorithm?
In technical terms, an algorithm is a finite set of unambiguous instructions that, when
followed, performs a specific task or solves a particular problem.

Think of it this way:

Data Structures are how you store information; Algorithms are the logic used to
manipulate that information.

1. Key Characteristics of a Good Algorithm


For a set of instructions to be considered a formal algorithm, it must meet these criteria (often
called the Finiteness, Definiteness, Input, Output, and Effectiveness rule):

●​ Finiteness: It must terminate after a limited number of steps. It cannot run in an infinite
loop.
●​ Definiteness: Each step must be precisely defined and clear (no "maybe" or "add a little
bit of salt").
●​ Input: It should have zero or more well-defined inputs.
●​ Output: It must produce at least one result (the solution).
●​ Feasibility: Each step must be basic enough to be performed with available resources
(time and memory).

2. How We Represent Algorithms


Before writing actual C++ code, programmers usually plan algorithms in two ways:

A. Pseudocode
This is a "fake" code that uses English-like phrases to outline logic without worrying about
semicolons or strict syntax.

Example (Finding the largest of two numbers):

1.​ Start
2.​ Input A and B
3.​ If A > B, then Print A
4.​ Else, Print B
5.​ End

B. Flowcharts
A visual representation using standard symbols:

●​ Ovals: Start / End


●​ Parallelograms: Input / Output
●​ Rectangles: Processes (calculations)
●​ Diamonds: Decisions (True/False questions)

3. Algorithm Analysis (Efficiency)


Not all algorithms are created equal. We measure them based on two main resources:

1.​ Time Complexity: How long does it take to run as the input size grows?
2.​ Space Complexity: How much memory (RAM) does it use?

In C++, we often use Big O Notation to describe this. For example, a simple loop through an
array of $n$ elements is called $O(n)$, meaning its time increases linearly with the number of
elements.

4. Real-World Example: Linear Search


If you want to find a specific number in a list, the algorithm looks like this:

The Problem: Find number $X$ in a list of $N$ items.

The Algorithm:

1.​ Start at the first item in the list.


2.​ Compare the current item with $X$.
3.​ If they match, return the position and Stop.
4.​ If they don't match, move to the next item.
5.​ Repeat steps 2–4 until you reach the end of the list.
6.​ If the end is reached and $X$ wasn't found, report "Not Found" and Stop.

5. Why are Algorithms Important?


In C++, your code is just the "translation." The algorithm is the "thought process." If your
algorithm is bad, your program will be slow or use too much memory, regardless of how well
you know C++ syntax.
Common types of algorithms you will encounter later include Sorting (arranging data),
Searching (finding data), and Hashing (encrypting or indexing data).

You might also like