>CBFalconer said:
>> for (i = 0; i <= 200000; i++) {
In article <7dmdnfY00vue8oPVRVnyuQA@[EMAIL PROTECTED]
>
Richard Heathfield <rjh@[EMAIL PROTECTED]
> wrote:
>Rookie error...
>> if (a[i] == item) break;
>> }
>> if ((i <= 200000) && (a[i] == item)) return i;
>...repeated.
Given the idea that the array has exactly 200000 elements, anyway.
In reality the "occupied size" would presumably be a variable.
In any case, this misses out on a handy trick that can be used when
the array has enough room. When a[] is the array to be searched and
n is "occupied size", if a[n] can be overwritten, one can use:
a[n] = item;
for (i = 0; a[i] != item; i++)
continue;
This removes one test (i < n) from the loop, and yet is guaranteed
to terminate the loop. Then one need only apply the "i < n" test
afterward.
(Also, even with the "i < n" test in the loop, there is no need to
re-test a[i]==item afterward.)
In reality, the original question is not very well formed, for
reasons that others have touched on. If the options include "sorting
the array", one has to consider how often the searches will happen
compared to the sort, and whether modifications to the array can
be done in ways that maintain the sorted order. If the array *is*
sorted, we can use binary or even interpolative searches rather
than linear searches. (Binary search is O(log n) and interpolative
search is O(log log n), in the mythical average case at least.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html


|
29 Posts in Topic:
|
mike-yue <needpassion@ |
2008-05-04 15:27:30 |
|
James Harris <james.ha |
2008-05-04 15:51:50 |
|
cri@[EMAIL PROTECTED]
(R |
2008-05-04 22:57:09 |
|
Richard Heathfield <rj |
2008-05-04 23:14:34 |
|
Peter Nilsson <airia@[ |
2008-05-04 16:34:38 |
|
mike-yue <needpassion@ |
2008-05-04 17:01:40 |
|
Richard Heathfield <rj |
2008-05-05 00:13:59 |
|
Ian Collins <ian-news@ |
2008-05-05 12:46:28 |
|
Keith Thompson <kst-u@ |
2008-05-04 17:26:57 |
|
Charlton Wilbur <cwilb |
2008-05-04 20:25:32 |
|
CBFalconer <cbfalconer |
2008-05-04 20:53:39 |
|
Richard Heathfield <rj |
2008-05-05 02:24:59 |
|
Chris Torek <nospam@[E |
2008-05-05 02:36:24 |
|
CBFalconer <cbfalconer |
2008-05-05 02:39:40 |
|
Richard Heathfield <rj |
2008-05-05 07:46:06 |
|
Keith Thompson <kst-u@ |
2008-05-05 01:25:54 |
|
CBFalconer <cbfalconer |
2008-05-05 18:59:13 |
|
Richard Heathfield <rj |
2008-05-06 02:39:15 |
|
CBFalconer <cbfalconer |
2008-05-05 22:52:39 |
|
Richard Heathfield <rj |
2008-05-06 03:27:46 |
|
"rio" <a@[EM |
2008-05-05 18:58:38 |
|
Antoninus Twink <nospa |
2008-05-05 11:15:51 |
|
James Harris <james.ha |
2008-05-05 03:43:01 |
|
mike-yue <needpassion@ |
2008-05-05 10:18:30 |
|
Keith Thompson <kst-u@ |
2008-05-05 12:50:07 |
|
mike-yue <needpassion@ |
2008-05-05 10:24:37 |
|
mike-yue <needpassion@ |
2008-05-05 10:28:50 |
|
mike-yue <needpassion@ |
2008-05-05 14:03:30 |
|
CBFalconer <cbfalconer |
2008-05-05 18:53:17 |
|