Special

Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! Xojo Bundle

This bundle includes six back issues of the magazine -- all of year 21 in printed book and digital formats -- plus a one-year subscription (beginning with 22.1) so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as $35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Article Preview


Buy Now

Issue 4.5

COLUMN

Searching Arrays and Lists

The Virtues of Brute Force

Issue: 4.5 (May/June 2006)
Author: Charles Yeomans
Author Bio: Charles is the author of "I Declare: Calling External Functions in REALbasic", available online at http://www.declareSub.com
Article Description: No description available.
Article Length (in bytes): 10,787
Starting Page Number: 38
Article Number: 4514
Related Web Link(s):

http://www.declareSub.com

Excerpt of article text...

Searching a list is a common task in REALbasic programming. Lists include not only arrays, but control arrays, the rows of a Listbox or MenuItem, etc. The first algorithm of choice for searching lists is sequential search, which works as follows: given a list and an object, compare each element of the array to the object and return the match index, if one is found; otherwise return -1.

Sequential search has two advantages. First, it is very simple to understand and to implement. For those looking for rules of thumb about data structures and algorithms, here is one attributed to Ken Thompson, co-creator of Unix.

"When in doubt, use brute force."

Sophisticated algorithms may or may not be faster, but they are always harder to implement and debug. The second advantage of sequential search is that, in general, it is the fastest possible algorithm.

To see why one cannot do any better, consider searching a randomly generated array of objects. Checking an element of an array for a match provides no information about the rest of the array. To do better, you need to impose more structure on the array; in particular, you could sort the array. But keep in mind that sorting an array is significantly slower than searching it, so presorting an array is usually only profitable for arrays that will be searched repeatedly.

...End of Excerpt. Please purchase the magazine to read the full article.