Q: Why do programmers always mix up Halloween and Christmas? A: Because Oct 31 == Dec 25!

References
[1] Beautiful Code: Leading Programmers explain how they think

the brown-dragon blog

Tricky Binary Search

2008-12-13

Ranga galloped out of the woods onto the clearing where Charlie and the brown dragon were arguing. His horse, a magnificent black Arabian, snorted in fear as it caught the acrid smell of dragon. Annoyed by its own instinctive terror, the proud stallion put on an extra burst of speed that delighted Ranga. He loved making a dramatic entrance.

Reining in next to the dragon and his pretty companion he swung off the back of the horse perfectly aware that he looked impressive. Charlie caught a whiff of perfume as he walked towards them smiling. She sighed. That smile meant nothing good.

"Hey," he called, "Good to see you both! I thought I'd find you here."

"Yeah, well, bully for you." growled the dragon. "What do you want?"

"No reason to be rude," he said, looking hurt. "I wanted to show you something, but if you can't be civil, I'll head back."

"No don't," said Charlie, feeling bad. "Dragon's just in a bad mood." She smiled at him to make up for dragon's behaviour then immediately regretted it.

Ranga grinned. "Fine then," he said. "I just wanted to show our good friend a bit of code I found[1]. How about it dragon?"

The dragon looked surly but Ranga knew he was interested.

"Take a look at this code I found for a binary search." he said.

BinarySearch.java

public class BinarySearch
{
   public static int search (int pForThis, int[] pInThis)
   {
           int     high, low, mid;

       // Set up bounds
       high = pInThis.length;
       low  = -1;

       // Bounce around array
       while (high - low > 1) {
           // Find the mid-point (taking care of integer wrap-around)
           mid = (high + low) >>> 1;
           // Adjust the range
           if (pInThis [mid] > pForThis) {
               high = mid;
           } else {
               low  = mid;
           }
       }

       // Check if we've found the value while taking care of edge conditions
       if (low == -1 || pInThis[low] != pForThis) {
           // Nope - signal "not found"
           return -1;
       }

       // Found! Return postion.
       return low;
   }
}

"So what do you think?" said Ranga.

"You mean besides the fact that it's written in Java?" snorted the dragon. "Aren't you tired of this rotten language yet?"

"No, not yet." Ranga winked at Charlie.

"Hmmpfh!" huffed the dragon looking at the code again. He spotted something and grinned widely, showing a nightmare of razor-sharp teeth. "Java encourages such sloppy programming!" he crowed delightedly. "Just look at this exit condition! Even if the search finds the variable the loop continues doing more comparisons! Idiotic! But then what do you expect with Java programmers? The language encourages lazy idiots!"

Ranga literally beamed. The idiot dragon had fallen right into it.

"Actually," he almost trilled. "There's a good reason for that."

"Eh? What reason?"

"When doing a search, for this to make a difference you'd have to have *a lot* of elements..."

"Oh!" interrupted the dragon, "That's your excuse for sloppy code? It's only sloppy when searching a lot of elements? Pah! It's rubbish like this that all you Java drones pump out that result in bloatware."

"Not at all." continued Ranga, unperturbed. "So now, since we have a large number of elements, what's the probability of finding the element in the first round of the loop?"

The dragon didn't answer immediately. He looked back at the code and Charlie saw his jaw muscle clenching as it dawned on him what Ranga was leading up to.

Ranga wasn't about to let him off the hook though. He waited patiently until the dragon muttered "Low."

"Right, it's obviously low. A fraction of a percent for anything above a 100 elements." said Ranga. "And it stays low until we reach say 5 or less elements."

"20% at five." rumbled the dragon.

"Exactly!" grinned Ranga. "So actually, the majority of the time, for a majority of the loop runs, doing an early break will actually introduce an additional check. So your early break check would actually result in less efficient code."

"Hey! That's smart!" blurted Charlie. She glanced at the dragon guiltily, but she really felt it was pretty cool.

"Yeah. It is smart." admitted the dragon grudgingly.

"Really?" exclaimed Ranga rubbing it in, "Smart? Java coders?"

"It is smart." admitted Charlie hastily, glancing at the dragon who looked like he was ready to burst. "Goes to show you just how little language matters. There are brilliant programmers in every language. Right dragon?"

The dragon mumbled something. It sounded rude. He had gotten the idea - he even thought it was brilliant. But had made a complete fool of himself and he knew Ranga would milk it for all it was worth. Going to town for the next few weeks would be unbearable.

"Well, I must be off." said Ranga gleefully, "See you around sometime!"

Mounting his horse, he turned back and looked at Charlie. "Why don't you back with me?" he asked, "There's obviously nothing this bigot of a dragon can teach you anyway"

"That's enough!" roared the dragon. "Go away!"

Ranga didn't even blink. He kept his gaze on Charlie and waited calmly.

"No thanks", said Charlie.

"Suite yourself." he shrugged. Turning his horse around he galloped off. At the edge of the wood he stopped and turned back.

"Oh crap!" whispered Charlie. She knew what was coming.

"You know," Ranga yelled, "My dad always told me that dragons were smelly, overgrown lizards with the brainpower of a moorhen! And you! You're the living proof!"

The dragon stared at him for an breathless instant. Then, with a terrible roar of rage, the mighty creature launched himself in a low dive smashing aside a tree and a rock for good measure, his jaw muscles working furiously as the superchargers on either side of his mouth drew in oxygen for his devastating flame. Ranga and his steed galloped off furiously.

"Males!" sighed Charlie.

Other Posts

(ordered by Tags then Date)