Sunday, March 12, 2017

What is an Algorithm ?

Algorithm is any well-defined computational procedure that takes some value, or set of values as input and produces some value, or set of values, as an output. An algorithm is thus a sequence of computational steps that transforms the input into the output. Algorithm is also a tool for solving a well-specified computational problem. The statement of the problem specifies in general terms the desired input/output relationship. The algorithm describes a specific computational procedure for achieving that input/output relationship.
Example 1 – a sorting problem. Let’s say that we have a list or sequence of unsorted numbers and our task is to sort them out into non-decreasing order. Here is how the sorting problem is defined:
  • Input: a sequence of n numbers a1,a2,a3,…,an
  • Output: a permutation or ordering of the input sequence such that a1’ < a2’ < a3’ < an.

Let’s define a list of unsorted numbers in Python:
>>> l1 = [3324,212,3214,45, 500,213,12,34]
>>> l1.sort()
>>> l1
[12, 34, 45, 212, 213, 500, 3214, 3324]
As you can see first we’ve defined list l1 and then we’ve used a built-in Python function sort() without any arguments. The result of sorting is what we’ve been looking for and that is an ordered list from the lowest number 12 to the highest number 3324.
Sorting is a fundamental operation in computer science, and as a result a large number of good sorting algorithms have been developed. Which algorithm is best for given application depends on:
  • Number of items to be sorted
  • The extent to which the items are already sorted
  • Possible restrictions on the item values.
  • Kind of storage device to be used such as memory, disk or other.

Algorithm is correct if, for every input it halts with the correct output. We say that the correct algorithm solves a given computation problem. Incorrect algorithm might not halt at all on some input instances, or it might halt with an answer other than the desired one. Sometimes the incorrect algorithms might be useful if their error rate can be controlled.
In English language an algorithm might be specified as computer program or hardware design but it must provide precise description of the computational procedure to be followed.

1 comment:

  1. Simple and nice explanation. Algorithms are really important to develop problem-solving skills as well as cracking the coding interview. Great blog.

    ReplyDelete