Selection_Sort.ads
package Selection_Sort is
type Array_T is array (Natural range <>) of Integer; -- Array type
procedure Sort (Array_To_Sort : in out Array_T); -- Sort the array
procedure Print (Array_To_Print : in Array_T); -- Output the array
end Selection_Sort;
Selection_Sort.adb
with Ada.Text_IO;
package body Selection_Sort is
procedure Sort (Array_To_Sort : in out Array_T) is
begin
for i in Array_To_Sort'Range
loop
declare
minimum_index : Natural := i;
begin
for j in i+1 .. Array_To_Sort'Last
loop
if Array_To_Sort(j) < Array_To_Sort(minimum_index) then
minimum_index := j;
end if;
end loop;
if minimum_index /= i then
-- Swap
declare
temp : Integer := Array_To_Sort(minimum_index);
begin
Array_To_Sort(minimum_index) := Array_To_Sort(i);
Array_To_Sort(i) := temp;
end;
end if;
end;
end loop;
end Sort;
procedure Print (Array_To_Print : in Array_T) is
begin
Ada.Text_IO.Put ("Array_To_Print: (");
for i in Array_To_Print'Range
loop
Ada.Text_IO.Put(Integer'Image(Array_To_Print(i)));
end loop;
Ada.Text_IO.Put_Line(")");
end Print;
begin
null;
end Selection_Sort;
Main.adb
with Selection_Sort;
with Ada.Text_IO;
with Ada.Exceptions;
procedure Main is
--My_Array : Selection_Sort.Array_T := (77,6,7,3,4,11,60,23,34,11);
--My_Array : Selection_Sort.Array_T := (1,2,3,4,5,6,7,8,9,10);
My_Array : Selection_Sort.Array_T := (10,9,8,7,6,5,4,3,2,1);
begin
Selection_Sort.Print(Array_To_Print => My_Array);
Selection_Sort.Sort(Array_To_Sort => My_Array);
Selection_Sort.Print(Array_To_Print => My_Array);
exception
when E: others =>
Ada.Text_IO.Put_Line("Exception: " &
Ada.Exceptions.Exception_Information(E));
end Main;
我覺得它可能會幫助,如果你描述ŧ他用Ada的近似*算法,並且不太在意語法。現在,您正嘗試創建一個20'99999'的數組(技術上已分類)。 –
想象一下,值(1)是一些數字12000例如和價值(2)是15000例如,等等......這就是我想要排序,價值(我),我是1-20其中值的每次迭代(I)是一個不同的值。 – Dibs
您的問題已經通過鏈接到維基百科文章的選擇排序進行編輯。這應該有所幫助。 –