Saturday, November 20, 2021

Algorithms in Programming

 Algorithms in Programming

Algorithms are most common thing in programming and we use algorithms in our day today life as well. Lets here talk about algorithms in programming.

An algorithm is a  a set of steps used to complete a specific task. They're the building blocks for programming and they allow things like computers, smartphones, and websites to function and make decisions.

There are five important characteristics of an algorithm 

  1. Well-ordered. 
  2. Has unambiguous operations. 
  3. Has effectively computable operations. 
  4. Produces a result. 
  5. Halts in a finite amount of time. 
When we are given an algorithm there are several ways to determine the purpose of the algorithm. Sometimes it is clear that the algorithm is simple; however, at other times it is useful to "dry out" the algorithm to see what happens.

Running an algorithm dry means assigning the values to the variables of an algorithm and performing the processing without translating them into code. For that we can use Trace Tables (Desk Checking).

Algorithms can be designed using pseudo-code or flowcharts or both. It focuses on step logic rather than programming language.


Pseudo-Code

Pseudocode is a simple way of describing a set of instructions in a way that is similar to a programming language. In the algorithm, most operations fall into three main categories:

  • inputs
  • processes
  • outputs

Example for pseudo-code for printing stars like this

First lets see the scenario. In first line we need to print one star. then go to next line and print two stars. Then again go to next line and print three stars. Then again go to next line and print four stars and finally go to next line and print five stars.

We can implement above scenario like this
  1. Start
  2. Declare counter
  3. Initialize counter=0
  4. WHILE counter<5
  5. counter=counter+1
  6. FOR  counter number of times
  7. Display "*"
  8. END FOR
  9. Print new line
  10. END WHILE
  11. Stop

1st line-    Start point of the pseudo-code

2nd line-   Declare variable for remind counting

3rd line-    Initialize that variable

4th line-    Start a While loop until counter <5

5th line-    counter variable increment by 1

6th line-    Start a For loop and run it times of counter variable

7th line-    Display star

8th line-    End of the For loop

9th line-    Go to new line

10th line-   If the While loop condition is false End the While loop

11th line-   End point


We can also use Trace Tables (Desk Checking) tables to show how the repetition lines work




Flowcharts

A flowchart is a diagram that shows an overview of an algorithm. Flowcharts use a variety of standard symbols to represent different elements and arrows to show flow or direction. These symbols are used to build the flowchart and show the step by step solution to the problem.

Common flow diagram symbols


Lets draw a flowchart for above example




Efficiency of algorithms

Efficiency looks at how long it takes to run a particular algorithm and how much storage space is required. By using both measures, a seemingly much more complex algorithm can be more efficient.


To overcome challenges, stop comparing yourself to others - Dean Furness

When we are young with our height and weight, as we grow it becomes our speed and our strength. It seems as if personal averages is almost ...