[Gabriel White]: And I am also confused by your comment of Jean-Francois' code using a linear search algorithm. This code does not sort from the middle of the string, it spans the entire string. A linear search algo runtime is O(log n) and not O(n). I'm trying to make some sense of this, but I'm not having any luck. Jean-Francois showed a linear search, which is O(n). (n being the number of elements in the array.) A binary search would be O(log(n)). Of course, if the array isn't already sorted then this doesn't help much. However, if you're going to run many saerches of the same array, there's an even better solution:
Using a HashMap this way will take a bit longer to set up initially - but each subsequent search will be O(1). This can be a big savings if n is large and you have lots of searches. For an array of just a few items, it's not worth the trouble - but if you have an array of thousands of items, using a HashMap will make a big difference.