Coming to the realization that I have programming heroes was another of those little epiphanies that I have had over the last little while. One of these cerebral super-heroes is Dave Thomas of Pragmatic programmer fame. His articles on Code Kata inspired me to take up the practice myself. In fact, the second of my exercises is the second in those he sets out, a binary sort. (I must admit I like the martial arts theme he uses in calling it a Karate Chop!) The practice as he set it out is quite a tough one; to find five different implementations over the course of five days.

Specification

Write a binary chop method that takes an integer search target and a sorted array of integers. It should return the integer index of the target in the array, or -1 if the target is not in the array. The signature will logically be:

chop(int, array_of_int) -> int

You can assume that the array has less than 100,000 elements. For the purposes of this Kata, time and memory performance are not issues (assuming the chop terminates before you get bored and kill it, and that you have enough RAM to run it).

Advertisements