使用Ada进行选择排序

使用Ada进行选择排序,ada,Ada,我将如何按照升序值的顺序实现对整数数组的使用?我相信语法会包含for循环,但我不确定语法是如何工作的 for I in 1..20 loop TempLowest : Integer := 99999; if Value(I) < TempLowest then Value(I) := TempLowest; end if; end loop; 用于1..20循环中的I TempLowest:整数:=99999; 如果值(I)

我将如何按照升序值的顺序实现对整数数组的使用?我相信语法会包含for循环,但我不确定语法是如何工作的

for I in 1..20 loop
   TempLowest : Integer := 99999;
   if Value(I) < TempLowest then
      Value(I) := TempLowest;
   end if;
end loop;
用于1..20循环中的I
TempLowest:整数:=99999;
如果值(I)
我想象它是这样的,但我不太明白这将如何按照从降序到升序的顺序组织我的数组。谢谢你的帮助

这篇文章对我帮助不大;我理解基本概念,但Ada的语法本质上是我的问题


比较您熟悉的,并查看Ada标签标签中引用的资源。如果您遇到问题,请在您的问题中加入一个显示您修改方法的选项。

选择\u 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;

我认为,如果您用Ada的近似值来描述算法,并且少担心语法,可能会有所帮助。现在,您正试图创建一个由20个
99999
s组成的数组(从技术上讲是排序的)。假设值(1)是12000,值(2)是15000,等等。。。这就是我想要排序的,值(I),I为1-20,其中值(I)的每次迭代都是不同的值。你的问题已经通过维基百科关于选择排序的文章链接进行了编辑。这应该会有帮助。嗯,这对我没有多大帮助,我理解基本概念,但ada的语法本质上是我的问题。如果你不理解ada,请单击问题下方的[ada]标记,然后单击信息,这将带你进入一个包含各种学习资源的页面。
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;
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;