Ada searching
last modified November 23, 2026
A search answers whether a value is present and, if it is, where. Linear search looks at every element and works on any array. Binary search halves a sorted array at each step and is much faster, but it is only correct on ordered data. This article writes both, counts their comparisons, and turns binary search into a generic procedure that can search any sorted array, including an array of records.
Linear search
The function scans the array from its first index and returns as soon as it
finds the value. If the loop ends without a match it returns zero. An
unconstrained array parameter carries its own bounds, so A'Range
adapts to every actual argument.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Linear_Search is
type Numbers is array (Positive range <>) of Integer;
Data : constant Numbers := [4, 9, 2, 7, 5, 8];
function Find (A : Numbers; Value : Integer) return Natural is
begin
for I in A'Range loop
if A (I) = Value then
return I;
end if;
end loop;
return 0;
end Find;
begin
Put_Line ("7 is at index" & Integer'Image (Find (Data, 7)));
Put_Line ("3 is at index" & Integer'Image (Find (Data, 3)));
end Linear_Search;
The return type is Natural rather than the array's index subtype,
because zero is used as the "not found" result and zero is not a valid index.
The search examines four elements before it finds 7, and all six before it
gives up on 3. In the worst case it touches every element.
$ gnatmake -gnat2022 -gnatwa linear_search.adb $ ./linear_search 7 is at index 4 3 is at index 0
Binary search
Binary search keeps a range of candidate indices. It compares the middle element with the target; if they are equal the search is over, if the middle is too small the lower half is discarded, and otherwise the upper half is. Each step halves the range, so a hundred elements need at most seven comparisons.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Binary_Search is
type Numbers is array (Positive range <>) of Integer;
Data : Numbers (1 .. 100);
Target : constant := 94;
Comparisons : Natural := 0;
function Find (A : Numbers; Value : Integer) return Natural is
Low : Integer := A'First;
High : Integer := A'Last;
begin
while Low <= High loop
declare
Mid : constant Integer := Low + (High - Low) / 2;
begin
Comparisons := Comparisons + 1;
if A (Mid) = Value then
return Mid;
elsif A (Mid) < Value then
Low := Mid + 1;
else
High := Mid - 1;
end if;
end;
end loop;
return 0;
end Find;
begin
for I in Data'Range loop
Data (I) := I;
end loop;
Put_Line ("found at" & Integer'Image (Find (Data, Target)));
Put_Line ("comparisons =" & Integer'Image (Comparisons));
end Binary_Search;
The middle is computed as Low + (High - Low) / 2 rather than
(Low + High) / 2, so a large pair of bounds cannot overflow before
the division. Low and High are Integer,
not Positive, because High := Mid - 1 can legitimately
become zero when the target is below the first element. Searching for 94 takes
four comparisons, while a linear search would take ninety-four.
$ gnatmake -gnat2022 -gnatwa binary_search.adb $ ./binary_search found at 94 comparisons = 4
A generic search
The generic procedure takes the element type, the index type, the array type,
and a comparison as formals. It never needs an equality operator: two values
are equal when neither is less than the other. Index arithmetic uses
'Pos and 'Val, so the same body works with integer
indices, enumeration indices, or any other discrete index type.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Generic_Search is
generic
type Element_Type is private;
type Index_Type is (<>);
type Array_Type is array (Index_Type range <>) of Element_Type;
with function "<" (Left, Right : Element_Type) return Boolean is <>;
procedure Search (A : Array_Type; Key : Element_Type;
Found : out Boolean; Index : out Index_Type);
procedure Search (A : Array_Type; Key : Element_Type;
Found : out Boolean; Index : out Index_Type) is
Low : Integer := Index_Type'Pos (A'First);
High : Integer := Index_Type'Pos (A'Last);
Mid : Integer;
Pos : Index_Type;
Same : Boolean;
begin
Found := False;
Index := A'First;
while Low <= High loop
Mid := Low + (High - Low) / 2;
Pos := Index_Type'Val (Mid);
Same := not (A (Pos) < Key) and then not (Key < A (Pos));
if Same then
Found := True;
Index := Pos;
return;
elsif A (Pos) < Key then
Low := Mid + 1;
else
High := Mid - 1;
end if;
end loop;
end Search;
type Numbers is array (Positive range <>) of Integer;
Data : constant Numbers := [11, 23, 35, 47, 59, 71];
procedure Find is new Search (Integer, Positive, Numbers);
Found : Boolean;
Where : Positive;
begin
Find (Data, 47, Found, Where);
Put_Line ("47 found =" & Boolean'Image (Found)
& ", index =" & Integer'Image (Where));
Find (Data, 40, Found, Where);
Put_Line ("40 found =" & Boolean'Image (Found));
end Generic_Search;
Search reports two results: Found says whether the
key is present, and Index gives its position. When the key is
absent, Index keeps the first index as a harmless valid value; the
caller must test Found before using it. Same compares
both directions, which is why a single "<" is enough.
$ gnatmake -gnat2022 -gnatwa generic_search.adb $ ./generic_search 47 found =TRUE, index = 4 40 found =FALSE
Searching records
The same generic procedure works for a record type once the record has a
comparison. The roster is sorted by name, and Before compares the
name components. The key is a record aggregate whose age is ignored by the
comparison, so any age can be supplied. The generic declaration and body from
the previous section are unchanged; the listing shows the new declarations and
the main program around them.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Record_Search is
-- The generic Search from generic_search.adb is declared here.
type Person is record
Name : String (1 .. 5);
Age : Integer;
end record;
type People is array (Positive range <>) of Person;
Roster : constant People := [("ALICE", 31), ("BOB ", 24),
("CAROL", 45), ("DAVE ", 38),
("ERIN ", 29)];
function Before (A, B : Person) return Boolean is (A.Name < B.Name);
procedure Find is new Search (Person, Positive, People, Before);
Found : Boolean;
Where : Positive;
begin
Find (Roster, ("CAROL", 0), Found, Where);
Put_Line ("CAROL found =" & Boolean'Image (Found)
& ", index =" & Integer'Image (Where));
Put_Line ("age =" & Integer'Image (Roster (Where).Age));
Find (Roster, ("FRANK", 0), Found, Where);
Put_Line ("FRANK found =" & Boolean'Image (Found));
end Record_Search;
The instantiation supplies Before as the fourth actual, which
replaces the default "<". The searched record is
("CAROL", 0); the zero age never matters because only the name is
compared. After a successful search, Roster (Where).Age reads the
stored record at the returned index.
Binary search is the standard way to look up a key in a sorted table, but it requires the table to stay sorted. If values are inserted and removed often, a hash table or a balanced tree is a better choice; later articles on containers and hash tables return to that trade-off.
$ gnatmake -gnat2022 -gnatwa record_search.adb $ ./record_search CAROL found =TRUE, index = 3 age = 45 FRANK found =FALSE
Next steps
The next article builds linked lists: nodes connected by access values, insertion at the head and the tail, deletion from the middle, reversal in place, and a small package that owns and frees its nodes. The access types article introduces the pointers that those structures depend on.
Source
In this article we have searched arrays linearly and by halving, counted the
comparisons of both methods, written a generic binary search with a formal
"<", and used it on an array of records with a custom
comparison. The generics article explains the
formal types and subprograms used by the generic search.
Author
List all Ada tutorials.