home *** CD-ROM | disk | FTP | other *** search
- -- ++
- -- Program to test the shell sort generic.
- -- An array of pointers to records is sorted via shell sort. The pointers
- -- are swapped rather than swapping the record for speed.
- -- --
-
- with Calendar;
- with Integer_IO;
- with Positive_IO;
- with Random;
- with Shell_Sort;
- with Text_IO;
- procedure Shell_Sort_Test is
-
- Count : Positive;
- Num : Positive;
-
- procedure Shell_Test ( Num : in Positive; Count : in Positive ) is
- type Element is
- record
- Key1 : Integer;
- Key2 : Integer;
- end record;
- type Key_Index is new Positive range 1 .. Num;
- subtype Index is Positive range 1 .. Num;
- type Sort_Array is array (Key_Index range <>) of Element;
- type Sort_Array_Pointers is array (Index range <>) of Key_Index;
-
- Keys : Sort_Array(Key_Index);
- Key_Pointers : Sort_Array_Pointers(Index);
- Time : Calendar.Time;
- CPU : Duration;
-
- function "<" ( L : in Key_Index; R : in Key_Index ) return Boolean is
- begin
- return ( Keys(L).Key1 < Keys(R).Key1 ) OR ELSE
- ( ( Keys(L).Key1 = Keys(R).Key1 ) AND THEN
- ( Keys(L).Key2 < Keys(R).Key2 ) );
- end "<";
- pragma INLINE ("<");
-
- package Sort_Test is new Shell_Sort ( Item => Key_Index,
- Index => Index,
- Items => Sort_Array_Pointers,
- "<" => "<" );
- begin
- for I in Key_Index loop
- Keys(i).Key1 := Random.Random MOD 360;
- Keys(i).Key2 := Random.Random MOD 1000;
- end loop;
-
- time := Calendar.Clock;
- for J in 1 .. Count loop
- for I in Index loop
- Key_pointers(I) := Key_Index(I);
- end loop;
- Sort_Test.Sort ( Key_pointers );
- end loop;
- CPU := Calendar."-" ( Calendar.Clock, time);
- Text_IO.New_Line;
- Text_IO.Put ( "Time to sort " );
- Integer_IO.Put ( Num );
- Text_IO.Put ( " items = " );
- Integer_IO.Put ( Integer(CPU*1000)/count );
- Text_IO.Put_Line ( "ms" );
-
- for I in Index loop
- Integer_IO.Put ( Keys(Key_pointers(I)).Key1 );
- Integer_IO.Put ( Keys(Key_pointers(I)).Key2 );
- Text_IO.New_Line;
- end loop;
-
- end Shell_Test;
-
- begin
- Text_IO.Put ( "Number of elements in array to be sorted ? " );
- Positive_IO.Get ( Num );
- Text_IO.Put ( "Number of sorts to perform ? " );
- Positive_IO.Get ( Count );
- Shell_Test ( Num, Count );
- end Shell_Sort_Test;
-