## Algorithm and Code

May 25, 2009 Leave a comment

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:

- Some logical action
- Some Decision making
- 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.

- Define Problem Statement:

Find matching number in a list of ordered numbers. - Assumption:

List of ordered numbers is available. - Algorithm:
- Pick the middle number from the list.
- Compare with number to find.
- If number to find is smaller than the middle number, then number may exist in list with numbers smaller than middle number (Step 1).
- Use smaller number list to search number in.
- Go to Step 1.

- If number to find is larger than the middle number, then number may exist in list with numbers larger than middle number (Step 1).
- Use larger number list to search number in.
- Go to Step 1.

- 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.

- How big is our list likely to get?

How large will our list be, 100, 1000, 100000 - What type of numbers will we be comparing?

32 Bit Integer, 64 Bit Integer, Fractional Values - What limitations do we have from hardware?

Memory Limitations, Address Space, etc. - What limitations do we have from the compiler/interpreter?

Code Optimization, Interpreter memory space (e.g. browser) - 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.

- What happens when list has zero numbers.
- What happens when list size approaches the maximum number we use to address list?
- What happens when the list population does not have number that we are looking for?
- 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 - 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.