How fast can you make linear search?
Posted
by Mark Probst
on Stack Overflow
See other posts from Stack Overflow
or by Mark Probst
Published on 2010-04-30T01:50:25Z
Indexed on
2010/04/30
1:57 UTC
Read the original article
Hit count: 379
I'm looking to optimize this linear search:
static int
linear (const int *arr, int n, int key)
{
int i = 0;
while (i < n) {
if (arr [i] >= key)
break;
++i;
}
return i;
}
The array is sorted and the function is supposed to return the index of the first element that is greater or equal to the key. They array is not large (below 200 elements) and will be prepared once for a large number of searches. Array elements after the n-th can if necessary be initialized to something appropriate, if that speeds up the search.
No, binary search is not allowed, only linear search.
© Stack Overflow or respective owner