The Importance of Algorithms
The Importance of Algorithms
The Importance of Algorithms
Manjunath.R
*Website: http://www.myw3schools.com/
Abstract
A Computer Program can be viewed as an elaborate algorithm and algorithms are very important in Computer
Science for solving a problem -- based on conducting a sequence of specified actions. The best chosen
algorithm usually means a small procedure that solves a recurrent problem and makes sure computer will do
the given task at best possible manner. In cases where efficiency matters -- a proper algorithm is really vital to
be used. An algorithm is important in optimizing a computer program according to the available resources –
often play a very significant part in the structure of artificial intelligence, where simple algorithms are used in
simple applications, while more complex ones help frame strong artificial intelligence.
The best programs are written so that computing machines can perform them
quickly and so that human beings can understand them clearly. A programmer is
ideally an essayist who works with traditional aesthetic and literary forms as well
Donald Knuth
Introduction
You might have an algorithm for getting from office to home, for making a chunk of code that calculates
the terms of the Fibonacci sequence, or for finding what you’re looking for in a retail store. Algorithms
are the building blocks of computer programs or sequence of unambiguous instructions (the term
'unambiguous' indicates that there is no room for subjective interpretation) that tells how the problem
could be addressed and solved -- which is definitely overblown in their importance like road maps for
accomplishing a given, well-defined automated reasoning task -- which always have a clear stopping
point.
Long division and column addition are examples that everyone is familiar with -- even a simple function
for adding two numbers is implementation of a particular algorithm. Online grammar checking uses
algorithms. Financial computations use algorithms. Robotic field uses algorithms for controlling their
robot using algorithms. An encryption algorithm transforms data according to specified actions to
protect it. A search engine like Google uses search engine algorithms (such as, takes search strings of
keywords as input, searches its associated database for relevant web pages, and returns results). In fact, it
is difficult to think of a task performed by your computer that does not use computer rules that are a lot
like a recipes (called algorithms).
The use of computer algorithms (step-by-step techniques used for Problem-solving) plays an essential
role in space search programs. Scientists have to use enormous calculations, and they are managed by
high-end supercomputers, which are enriched with detailed sets of instructions that computers follow to
arrive at an answer. Algorithms have applications in many different disciplines from science to math to
physics and, of course, computing -- and provide us the most ideal option of accomplishing a task. Here
is some importance of algorithms in computer programming.
To improve the effectiveness of a computer program: An algorithm (procedure or formula for
solving a problem, based on conducting a sequence of specified actions) can be used to improve
1
the speed at which a program executes a problem and has the potential of reducing the time that a
program takes to solve a problem.
Proper usage of resources: The right selection of an algorithm will ensure that a program
consumes the least amount of memory. Apart from memory, the algorithm can determine the
amount of processing power that is needed by a program.
The algorithm to add two numbers entered by user would look something like this:
Step 1: Start
Step 2: Declare variables num1, num2 and sum
Step 3: Read values num1 and num2
Step 4: Add num1 and num2 and assign the result to sum
Two of these algorithms accomplish exactly the same goal, but each algorithm does it in completely
different way to achieve the required output or to accomplish our task. In computer programming, there
are often many different ways – algorithms (any well-defined computational procedure that takes some
value, or set of values, as input and produces some value, or set of values as output) -- to accomplish any
given task. Each algorithm has credits and demerits in different situations. If you have a million integer
values between -2147483648 and +2147483647 and you need to sort them, the bin sort is the accurate
algorithm to use. If you have a million book titles, the quick sort algorithm might be the best choice. By
knowing the toughness and weaknesses of the different algorithms, you pick the best one to accomplish a
specific task or to solve a specific problem.
One of the most important aspects of an algorithm is how fast it can manipulate data in various ways,
such as inserting a new data item, searching for a particular item or sorting an item. It is often easy to
come up with a list of rules to follow in order to solve a problem, but if the algorithm is too slow, it's back
2
to the drawing board. Efficiency of an algorithm depends on its design and implementation. Since every
procedure or formula for solving a problem based on conducting a sequence of specified actions -- uses
computer resources to run -- execution time and internal memory usage are important considerations to
analyze an algorithm.
3
Mohammed ibn-Musa al-Khwarizmi, who was part of the royal court in Baghdad and who lived from
about 780 to 850. On the other hand, it turns out algorithms (widely recognized as the foundation of
modern computer coding) have a long and distinguished history stretching back as far as the Babylonians.
Although there is some available body of facts or information about early multiplication algorithms in
Egypt (around 1700-2000 BC) the oldest algorithm is widely recognized to be valid or correct to have
been found on a set of Babylonian clay tablets that date to around 1600 - 1800 BC. Their exact
significance only came to be revealed or exposed around 1972 when an American computer scientist,
mathematician, and professor emeritus at Stanford University
Donald E. Knuth published the first English translations of various Babylonian cuneiform mathematical
tablets.
Here are some short extracts from his 1972 manuscript that explain these early algorithms:-
"The calculations described in Babylonian tablets are not merely the solutions to specific individual
problems; they are actually general procedures for solving a whole class of problems." - Pages 672
to 673 of "Ancient Babylonian Algorithms".
The wedge-shaped marks on clay tablets also seem to have been an early form of instruction manual:-
"Note also the stereotyped ending, 'This is the procedure,' which is commonly found at the end of
each section on a table. Thus the Babylonian procedures are genuine algorithms, and we can
commend the Babylonians for developing a nice way to explain an algorithm by example as the
algorithm itself was being defined...." - Pages 672 to 673 of "Ancient Babylonian Algorithms".
The use of computers, however, has raised the use of algorithms in daily transactions (like accessing an
automated teller machine (ATM ), booking an air or train or buying something online) to unprecedented
levels of real-world problems with solutions requiring advanced algorithms abounds. From Google
4
search to morning routines, algorithms are ubiquitous in our everyday life -- and their use is only likely to
grow to break down tasks into chunks that can be solved through specific implementations. Many of the
problems, though they may not seem realistic, need the set of well-defined algorithmic knowledge that
comes up every day in the real world. By developing a good understanding of a series of logical steps in
an algorithmic language, you will be able to choose the right one for a problem and apply it properly.
Different algorithms play different roles in programming – and algorithms are used by computer
programs where a program –
To give you a better picture, here is the most common type of algorithms:
Searching Algorithms
Sorting Algorithms
Path finding Algorithms
Tree and graph based algorithms
Approximate Algorithms
Compression Algorithms
Random Algorithms
Pattern Matching
Sequence Finding and a lot more
You only need to define your problem then select the right algorithm to use. The word algorithm may
not appear closely connected to kids, but the truth is that -- for kids -- understanding the process of
building a step by step method of solving a problem helps them build a strong foundation in logical
thinking and problem solving. Here are some problems you can ask your kid to discuss algorithmic
solutions with you:
Algorithms has shown it can yield results in all industries — from predicting insurance sales opportunities
and generating the millions of search inquiries every day to automating medicine research, optimizing
transportation routes, and much more. While algorithms help companies like Master Card and Visa to
keep their users' information, such as card number, password, and bank statement safely -- algorithms
aren't perfect. They fail and some fail spectacularly. Over the past few years, there have been some
serious fails with algorithms, which are the formulas or sets of rules used in digital decision-making
processes. Now people are questioning whether we're putting too much trust in the algorithms. When
algorithms go bad: Online failures show humans are still needed. Disturbing events at Facebook,
Instagram and Amazon reveal the importance of context.
5
Scripting language Programming language
Platform-specific Platform-agnostic (cross-platform)
Interpreted Compiled
Faster at runtime Slower at runtime
More code-intensive Less code-intensive
Creates standalone applications Creates applications as part of a stack
A step-by-step
procedure to get
Input Output
expected output from
the given input
Algorithm
checking the algorithm before its implementation checking the algorithm after its implementation
Design
Implement
6
Algorithm + Data structure = Program
The way in which various program data elements are organized and
stored into the memory so that the data can be used efficiently
Well-defined input
Desired output
Algorithm
Finiteness (not end up in an infinite loops)
Algorithms are not arbiters of objective truth and fairness simply because they're math. Program
― Zoe Quinn
7
Performance analysis of an algorithm depends on 2 factors:
Space Complexity: The amount of memory space required by an algorithm to complete its
task
Time Complexity: The amount of time required by an algorithm complete its task
The number of operations an algorithm performs to complete its task (considering that
each operation takes the same amount of time)
Data items are arranged in sequential order Data items are arranged in non-sequential order
(one after the other) (hierarchical manner)
Memory is not utilized in an efficient way Memory is utilized in an efficient way
Time complexity increase with the data Time complexity remains the same
size
Step 1: Start
Step 4: If a > b
If a > c
8
Display a is the largest number.
Else
Else
If b > c
Else
Step 5: Stop
The time taken by the computer to run code = number of instructions × time to execute each instruction
Factors that affect run time of an algorithm: Worst Case Complexity: The maximum time
Run time
Polynomial time
Superpolynomial time
9
Polynomial time → run time that does not increase faster than nk, which includes:
Superpolynomial time → run time that does increase faster than nk, which includes:
The run time of an algorithm → constant (doesn't increase) − no matter how large the
input size increases
The run time of an algorithm increases in direct proportion to the logarithm of the input
size
The run time of an algorithm increases in direct proportion to the input size
The run time of an algorithm increases in direct proportion to the input size squared
10
An algorithm is said to take cubic time (n3), if
The run time of an algorithm increases in direct proportion to the cube of the input size
The run time of an algorithm increases in direct proportion to the factorial of the input size
Whenever input size increases by 1, the running time increases by a factor of input size
The better the time complexity of an algorithm is, the faster the
The run time of an algorithm increases in direct proportion to the input size times the logarithm
of the input size
11
For any program, memory space is required for the following purposes:
Constant space complexity takes the same amount of memory space regardless of the input size (n)
#include<stdio.h>
int main()
{
int x = 4, y = 6, z;
z = x + y;
printf("%d", z);
return 0;
}
In the above program, 3 integer variables are used, hence they will take up 4 bytes
each, so the total space occupied by the above-given program is 4 × 3 = 12 bytes.
12
In this program, we have three integer variables. Therefore, this program always takes
12 bytes of memory space to complete its execution. And because this memory space
requirement is fixed for the above program, hence space complexity is said to be
constant space complexity or O(1) space complexity.
return sum;
}
array – the function's only argument – the space taken by the array is equal to 4n bytes
(where n is the length of the array)
size – a 4-byte integer
sum – a 4-byte integer
iterator – a 4-byte integer
The total memory space needed for this program to execute is 4n + 4 + 4 + 4 = 4n + 12 bytes − which
will increasing linearly with the increase in the input value n, hence it is called as linear space
complexity or O(n) space complexity.
13
To search an element in a given array, it can be done in two ways:
Linear search
Binary search
Linear Search:
Linear search is a very basic and simple search algorithm. In this type of search, a sequential
search is made over all elements one by one. Every element is checked and if a match is found
then that particular element is returned, otherwise the search continues till the end of the data
collection.
For Example:
8 10 12 15 17 20 25
17
8 10 12 15 17 20 25
17
8 10 12 15 17 20 25
17
14
Match not found
8 10 12 15 17 20 25
17
Match found
Element 17 is returned.
Linear search (whose running time increases linearly with the number of elements in the array.
For example if number of elements is doubled then, on average, the search would take twice as
long) is rarely used practically because other search algorithms such as the binary search
algorithm and hash tables allow significantly faster searching comparison to linear search.
Binary Search:
Binary Search is applied on the sorted array or list. In binary search, we first compare the value
with the elements in the middle position of the array. If the value is matched, then we return the
value. If the value is less than the middle element, then it must lie in the lower half of the array
and if it's greater than the element then it must lie in the upper half of the array. We repeat this
procedure on the lower (or upper) half of the array. Binary Search is useful when there are large
numbers of elements in an array.
15
We shall learn the process of binary search with a pictorial example. The following is our sorted
array and let us assume that we need to search the location of value 31 using binary search.
10 14 19 26 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
(high−low)
mid = low +
2
(9−0)
Here it is, 0 + = 4 (integer value of 4.5). So, 4 is the mid of the array.
2
10 14 19 26 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
Now we compare the value stored at location 4, with the value being searched, i.e. 31. We find
that the value at location 4 is 27, which is not a match. As the value is greater than 27 and we
have a sorted array, so we also know that the target value must be in the upper portion of the
array.
10 14 19 26 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
We change our low to mid + 1 and find the new mid value again.
low = mid + 1
16
(high−low)
mid = low +
2
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.
10 14 19 26 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
The value stored at location 7 is not a match; rather it is more than what we are looking for. So,
the value must be in the lower part from this location.
10 14 19 26 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
10 14 19 26 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
We compare the value stored at location 5 with our target value. We find that it is a match.
10 14 19 26 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
17
2 phases of Computer Programming Task
Implementation phase
Problem solving phase
Pseudocode Algorithm
A method of developing an algorithm A finite sequence of well-defined, computer-
implementable instructions, typically help to
simplify and understand the problem
Easy to understand, interpret and easier ease of Quite hard to understand and complex ease of
construction construction
Debugging
Debugging
Moderate
Simpler
Pseudocode to calculate the area of a circle Algorithm to calculate the area of a circle
AreaofCircle() 1. Start.
{ 2. Read the radius value r as the input given by
BEGIN the user.
Read: Number radius, Area; 3. Calculate the area as Area: 3.14 * r * r.
Input r; 4. Display the Area.
Area = 3.14 * r * r; 5. End.
Output Area;
END
}
18
Object-oriented programming aficionados
think that everything is an object.... this [isn't]
so. There are things that are objects. Things
that have state and change their state are
objects. And then there are things that are not
objects. A binary search is not an object. It is
an algorithm.
Alexander Stepanov
Bruce Schneier
19
[The Euclidean algorithm is] the granddaddy
of all algorithms, because it is the oldest
nontrivial algorithm that has survived to the
present day.
Donald Knuth
20
Data dominates. If you've chosen the right data
structures and organized things well, the
algorithms will almost always be self-evident.
Data structures, not algorithms, are central to
programming.
Rob Pike
Peter Norvig
Tim Berners-Lee
References
21