What are Algorithms?
Most of us have heard of the term ‘Algorithm’. According to the Oxford dictionary, an algorithm is a set of rules or steps to follow in calculations or problem solving operations, especially by a computer. Many things that we do in our day to day life can be equated to following an algorithm.
Daily examples of Algorithms:
Suppose we needed to whip up a strawberry milkshake, we would need to follow these steps:
Get all these ingredients: milk, sugar, whipped cream, strawberry, vanilla extract.
Chop the strawberries.
Add the strawberries, whipped cream and sugar in a mixer and blend it.
Add milk, vanilla extract to the mix and blend again.
Finally, the milkshake is ready to serve in a glass.
To illustrate let’s take another example, suppose we were using Google shopping to get a shirt for a 10 year old boy. We would first search for shirts and refine results to include only boys. Next we would look for the appropriate size (S, M, L, XL). Lastly we would narrow down with favored brands and prices.
Algorithms in Mathematics:
As we can see algorithms are a way of saying that to get to a particular outcome, solve a problem or compute something, we need to follow a sequence of instructions in a particular order. In mathematics, algorithms are widely used to solve word problems and complex problems involving multiple numerical operations. For instance, in a problem involving multiple numerical operations, the PEMDAS rule is followed to check which operation needs to be performed first and so on. Since algorithms are unambiguous and systematic, they can be efficiently used to solve complex problems by converting them into easy to follow instructions.
Algorithms in Computer Science:
Suppose we wanted to calculate the sum of n integers, we would use a ‘for’ loop in C programming language to get the numbers from the user and add the numbers to a variable called sum successively. The special way a program is written in the programming language is called syntax. The statement that follows ‘//’ is called comment. Let’s look at this program.
int n, sum=0, x, num; //this lines introduces three integer variables called n, sum, num and x
printf("How many numbers do you want to add?"); // this is a print statement
scanf("%d", &n); //here a function called scanf asks the user to enter the no of numbers they want to add
for (x = 0; x < n; x++) //this is called the for loop, it runs the code inside it how many ever times the user wants
{
scanf("%d", &num); //here the statement takes the numbers that need to be added from the user
sum = sum + num; //this just adds num to the sum successively till the loop runs
}
printf("Sum = %d\n", sum); //this statement just prints the sum
This is not the complete program but you can clearly see here how an algorithm is used to solve problems.
In Computer Science, any program written in a programming language, to solve a problem follows a sequence of well defined instructions. Algorithms can be coded as functions and further referenced by bigger programs/applications. Libraries are created to contain many commonly used functions, so users can readily use them in their programs and hence saving them a lot of time. A photo editing application could contain multiple functions (written based on algorithms) to work on images, such as cropping, filters, sharpening, rotating etc. To improve the performance of applications, programmers constantly seek to improve the efficiency of algorithms. The best functions/programs use minimal time and resources to perform tasks. Even search engines such as Google, use algorithms that rank websites according to multiple factors to list them in a search.