next up previous contents
Next: Structured Types Up: Lists and Arrays Revisited Previous: Sorting

Searching

The function SearchArray returns the index of an element in an array if it exists in the array. Otherwise it returns 0.
SearchArray(t :{string, real}, L :array);

> SearchArray(5, [1, 2, 7, 5, 8]);
4
> SearchArray('hi', ['hello', 'hallo', 'hey', 'hoi']);
0

The SearchOrderedArray function in Darwin searches an array A for a target value t but returns the index i such that $A[i] \leq t < A[i+1]$.
SearchOrderedArray(t : { string, real }, A : array)
If the list A is not ordered, the function returns the first such i satisfying the above inequality.

> SearchOrderedArray(5, [2, 4, 6, 8, 10]);
2
> SearchOrderedArray('xianghong', ['chantal', 'gaston', 'mike', 
>                                  'ulrike', 'xianghong']);
5
> SearchOrderedArray(5, [10, 8, 6, 4, 2]);             # the list is unordered.
0



Gaston Gonnet
1998-09-15