Algorithm and Code

What came first, the algorithm or the code?

To the modern day developers, there may or may not be a clear distinction between algorithm development and coding (as it is sometimes lovingly coined).

For me personally, the algorithm always comes first. It is the attempt to define in non-ambiguous terms what exactly needs to be done and in what order for achieving the desired result. Once I am satisfied with the algorithm, I then start to code it. There have been instances where I have had to revisit the algorithm and change it when problems have surfaced while coding or testing.

Afterall, I am only one person, and because of that, at times I can suffer ther classic mistake of miscalculation, bad assumption or plain oversight.

Once a person has designed a concrete algorithm, it can be implemented in almost any language. For me the two are to a great extent mutually exclusive. I know there might be a great number of softwere developers/engineers out there who will object. But I would like to clarify one thing.

Algorithm is independant of data types and language references. Why? Simply because, all problems have three aspects:

  1. Some logical action
  2. Some Decision making
  3. Some Mathematical action

These three actions are independant of the language of implementation. The choice of language will have it’s own representation of how to perform these three activities.

If one develops an algorithm for fast searching of numbers, e.g. The infamous and incredibally popular Binary Search, and descibes it in English. It would look like.

  1. Define Problem Statement:
    Find matching number in a list of ordered numbers.
  2. Assumption:
    List of ordered numbers is available.
  3. Algorithm:
    1. Pick the middle number from the list.
    2. Compare with number to find.
    3. If number to find is smaller than the middle number, then number may exist in list with numbers smaller than middle number (Step 1).
      1. Use smaller number list to search number in.
      2. Go to Step 1.
    4. If number to find is larger than the middle number, then number may exist in list with numbers larger than middle number (Step 1).
      1. Use larger number list to search number in.
      2. Go to Step 1.
    5. If number matches the middle number, then no need to search more.

Now we are ready to implement this algorithm. There are a number of things we need to conisder first.

  1. How big is our list likely to get? 
    How large will our list be, 100, 1000, 100000 
  2. What type of numbers will we be comparing? 
    32 Bit Integer, 64 Bit Integer, Fractional Values 
  3. What limitations do we have from hardware?
    Memory Limitations, Address Space, etc. 
  4. What limitations do we have from the compiler/interpreter?
    Code Optimization, Interpreter memory space (e.g. browser)
  5. Any other limitations?

I do understand that some limitations or restrictions may not be valid. In which case, you have one less thing to cater for.

Then we check conditions when this system may fail.

  1. What happens when list has zero numbers.
  2. What happens when list size approaches the maximum number we use to address list?
  3. What happens when the list population does not have number that we are looking for?
  4. What happens when the number we are looking lies between two numbers that are in contiguous spaces in the list.
    e.g. we looking for 5, but list is 1,2,3,4,6,7,8,9
  5. Any other way the implementation may fail.

Now we are ready to implement the algorithm.

Algorithms are not limited to technology, they are independant of it. They are related to solving problems. If a problem has been well throught through, the solution algorithm will not have any failings. A Verbatim translation of an algorithm will be guaranteed to fail at some point or the other, because it will not have catered to run time scenarios based on limitations of the implementation environment.

The algorithm is bad if it fails to solve the problem. The code is not to be blamed for algorithm failure. However, if algorithm has been checked and double checked, and the solution fails, then it’s a problem with the code not the algorithm.