An algorithm is a well-defined procedure that solves a problem or performs a computation by following a precise sequence of instructions. It’s akin to a recipe for processing data, where each step builds upon the previous one to produce a desired output. Algorithms can be found in various areas of IT, including mathematics and computer science, where they’re used to solve complex problems and optimize tasks.
In software development, algorithms are often used as specifications for data processing and play a crucial role in automated systems. They enable computers to perform tasks such as sorting large datasets, recommending content on social media, or even controlling the behavior of self-driving cars. By following a set of instructions, algorithms can execute complex computations with ease and accuracy.
The process begins with an initial input, which is then processed through a series of steps outlined in the algorithm. Each step builds upon the previous one, allowing the algorithm to produce an output that meets specific requirements. This output can take many forms, from a simple calculation to a sophisticated analysis or recommendation.
By understanding algorithms, individuals can unlock new possibilities in data analysis, artificial intelligence, and software development, ultimately driving progress in a rapidly changing technological landscape.
At the end of the day, one of the few things, and most powerful things a computer can determine if a statement (expression) is "true" or "false". Because true and false are so prevalent in decision making, they are their own keywords. In other words, in a program we write true not the string 'true'.
Logical Operators: AND, OR, and NOT
We can combine several "boolean" statements that have true/false meaning into a single statement using the key concepts AND and OR (and NOT). If I want to go to the movie AND I have enough money, then I will go to the movie. BOTH conditions have to evaluate to true (have to be true) before the entire expression is true.
OR is written with double "pipes" ||
AND is written with double "ampersands" &&
True is written: true;
False is written: false;
Not is written in a variety of ways. In Matlab it is the tilde (~). In C, Java, ActionScript, it is written as the exclamation point (!).
The truth tables
Truth tables show the result of combining any two expression boolean expressions using the AND operator and the OR operator (or the NOT operator).
An algorithm is a pre-programmed instruction set that, when activated, enables technology to take specific actions to resolve a challenge. Algorithms function through a process of input and output, where data entry prompts the system to analyze the information and execute the necessary commands to produce the desired outcome. For instance, a search algorithm responds to our search query by working to retrieve the relevant data stored within the data structure.
Algorithms can be represented in various forms, including natural languages, programming languages, pseudocode, flowcharts, and control tables. While natural language expressions are relatively uncommon due to their inherent ambiguity, programming languages are typically used for expressing algorithms that a computer can execute.
There are three constructs to an algorithm.
Linear sequence: The algorithm progresses through tasks or statements, one after the other.
Conditional: The algorithm makes a decision between two courses of action, based on the conditions set, i.e. if X is equal to 10 then do Y.
Loop: The algorithm is made up of a sequence of statements that are repeated a number of times.
Input: Bread (with butter or not), toaster, electricity
Instructions:
Place bread in the toaster.
Set the toasting level.
Lower the lever to start the toasting cycle.
Wait for 2-3 minutes until toast pops up.
Output: Toast!
This algorithm is simple and easy to follow, but it still demonstrates the basic structure of an algorithm:
Input: What you need to make toast (bread, toaster, electricity)
Instructions: A sequence of steps to take
Output: The result of following the instructions (toasted bread)
You can apply this algorithm in your daily life, and it works every time!
There are several types of algorithms, all designed to accomplish different tasks. For example, algorithms perform the following:
Search engine algorithm. This algorithm takes search stringsof keywords and operators as input, searches its associated database for relevant webpages and returns results.
Encryption algorithm. This computing algorithm transforms data according to specified actions to protect it. A symmetric key algorithm, such as the Data Encryption Standard, for example, uses the same keyto encrypt and decrypt data. As long as the algorithm is sufficiently sophisticated, no one lacking the key can decrypt the data.
Greedy algorithm. This algorithm solves optimization problems by finding the locally optimal solution, hoping it is the optimal solution at the global level. However, it does not guarantee the most optimal solution.
Recursive algorithm. This algorithm calls itself repeatedly until it solves a problem. Recursive algorithms call themselves with a smaller value every time a recursive function is invoked.
Backtracking algorithm. This algorithm finds a solution to a given problem in incremental approaches and solves it one piece at a time.
Divide-and-conquer algorithm. This common algorithm is divided into two parts. One part divides a problem into smaller subproblems. The second part solves these problems and then combines them together to produce a solution.
Dynamic programming algorithm. This algorithm solves problems by dividing them into subproblems. The results are then stored to be applied for future corresponding problems.
Brute-force algorithm. This algorithm iterates all possible solutions to a problem blindly, searching for one or more solutions to a function.
Sorting algorithm. Sorting algorithms are used to rearrange data structure based on a comparison operator, which is used to decide a new order for data.
Hashing algorithm. This algorithm takes data and converts it into a uniform message with a hashing
Randomized algorithm. This algorithm reduces running times and time-based complexities. It uses random elements as part of its logic.
A good algorithm must be correct, efficient, finite, and easy to implement.
Clearly specified input: We need to know all relevant details, such as input data type, input size, data structure, input distribution, etc. Input can be a single value or a set of values.
Clearly specified output: We need to know all relevant details related to the output. Output can be a single value or a set of values.
Correctness: For every set of inputs, our algorithm must halt with the correct output. In other words, our algorithm must be correct. An incorrect algorithm might halt with incorrect answers or not halt at the correct output for some input values.
Efficiency: Running time and memory are bounded resources, and our algorithms must use them wisely. In simple words, our algorithm should be efficient in terms of time and memory!
Finiteness: A good algorithm must terminate after a finite set of steps. We should avoid infinite recursion or infinite loop conditions.
Simplicity and elegance: An algorithm should be easy to understand and implement.
There are two main ways that algorithms can be represented – pseudocode and flowcharts.
Most programs are developed using programming languages. These languages have specific syntax that must be used so that the program will run properly. Pseudocode is not a programming language, it is a simple method of writing up a set of instructions for a computer program using plain English. This is a good way of planning a program before coding.
Writing in pseudocode is similar to writing in a programming language. Each step of the algorithm is written on a line of its own in sequence. Usually, instructions are written in uppercase, variables in lowercase and messages in sentence case.
In pseudocode, INPUT asks a question. OUTPUT prints a message on screen.
A simple program could be created to ask someone their name and age, and to make a comment based on these. This program represented in pseudocode would look like this:
OUTPUT 'What is your name?'
INPUT user inputs their name
STORE the user's input in the name variable
OUTPUT 'Hello' + name OUTPUT 'How old are you?'
INPUT user inputs their age STORE the user's input in the age variable
IF age >= 70 THEN
OUTPUT 'You are aged to perfection!'
ELSE
OUTPUT 'You are a spring chicken!'
In programming, > means ‘greater than’, < means ‘less than’, ≥ means ‘greater than or equal to’ and ≤ means ‘less than or equal to’.
You can also represent an pseudocode in the form of steps.
A flowchart is a diagram that represents a set of instructions.
Flowcharts normally use standard symbols to represent the different instructions.
There are few real rules about the level of detail needed in a flowchart.
Sometimes flowcharts are broken down into many steps to provide a lot of detail about exactly what is happening.
Sometimes they are simplified so that a number of steps occur in just one step.
A count down timer
Odd Even Number Checker
Prime Number Checker
Reverse a string
Login System
Simple Chatbot
Control a pump which fills water into a tank.
Pick a bus route with the shortest travel time.
Simulate a simple banking system. The user can deposit, withdraw, and check their balance.
Write an algorithm which allows you to find the number of occurences of a word in a paragraph.