我将如何按值递增的顺序实现selection sort对整数数组的使用?我相信语法会包含for循环,但我不确定语法是如何工作的。
for I in 1..20 loop
TempLowest : Integer := 99999;
if Value(I) < TempLowest then
Value(I) := TempLowest;
end if;
end loop;
我认为它是这样的,但我不太明白这将如何组织我的数组从下降到提升。谢谢你的帮助。
答案 0 :(得分:1)
selection sort文章并没有给我带来太多帮助;我理解基本概念,但Ada的语法基本上是我的问题。
将您熟悉的implementation cited与one in a language进行比较,并查看Ada代码info标签中引用的资源。如果您遇到问题,请edit您的问题,以包含显示您修订方法的Minimal, Complete, and Verifiable example。
答案 1 :(得分:0)
<强> 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;