Nerd For Tech
Published in

Nerd For Tech

Programming Algorithms

An algorithm is a series of well-defined instructions for solving a certain problem in computer programming. It takes a collection of inputs and outputs the desired result. As an example, An algorithm for adding two numbers is as follows:

  1. Take two number inputs(third variable for output)
  2. Add numbers using the + operator
  3. Display the result

Real-world example

you are in a Library, you want to read the book “The Art of Computer Programming” published: 2019, Author: Donald Knuth

note: Books are arranged year wise

You give yourself a set of instructions to find the book:

  1. Take the book one after the other.
  2. If the title of the book is The Art of Computer Programming, Pick the book and exit.
  3. If the title of the book is not The Art of Computer Programming, Repeat Step 01.

This set of instructions is the so-called Algorithm. You may question but why is an algorithm important? Because the best-chosen algorithm makes sure the computer will do the given task in the best possible manner.

Here we use the Linear Searching Algorithm (It sequentially checks each element of the list until a match is found or the whole list has been searched) to find the book,

this works fine and efficiently, but what if there are 100s or 1000s of books? it won’t be efficient enough.

That is where need an alternative Algorithm to make it efficient.

Let's check the Binary Search Algorithm(which is a search algorithm that finds the position of a target value within a sorted array) As the books are arranged in the order of years that can be used as an index, assuming there is only one book for each year(for simplification)

  1. Take the center book of the stack
  2. If the year of the book is less than 2019 go to the right stack and repeat step 1
  3. If the year of the book is greater than 2019 go to the right stack and repeat step 1
  4. If the year of the book is equal to 2019 exit

Binary Search Algorithm may involve in extra sorting process but when dealing with huge data queries it retrieves data much faster than Linear Search Algorithm.

“When efficiency matters a proper algorithm is really vital to be used.”

Qualities of Good Algorithms

  • Input and output must be well specified.
  • Each stage of the algorithm should be simple and straightforward.
  • Algorithms should be the most effective method of solving a problem out of a variety of options.
  • Computer code should not be included in an algorithm. Rather, the algorithm should be designed in a way that allows it to be utilized in a variety of programming languages.

Top 07 Algorithms Programmers should know

  1. Sort Algorithms
  2. Search Algorithms
  3. Hashing
  4. Dynamic Programming
  5. Binary Exponentiation
  6. String Parsing & Matching
  7. Primality Testing

Reference: https://www.programiz.com/dsa

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store