尝试在Ada中的简单链接列表上实现插入排序

时间:2018-05-03 00:03:18

标签: algorithm sorting ada insertion-sort

标题似乎不言自明...... 我已经研究了几个月的阿达。起初,我对整个程序一无所知,然后我一点一点地走了我的路。 下面的代码段是可编译的,结果如下:

5 2 16 20 8

2 5 8

第一行只是列表的内容,未分类。第二行是排序后的:排序,但不完整!而且我不太明白为什么特别是16和20被忽略了。 阅读"选择" as"插入"," suivant" as" next"。

with ada.text_io;
use ada.text_io;
procedure MAIN is
   subtype T_ELEMENT is NATURAL;
   type T_Cellule;
   type T_Liste is access T_Cellule;
   type T_Cellule is record
      Valeur  : T_Element;
      Suivant : T_Liste;
   end record; 

type TAB is array (Positive range <>) of Natural;

function Create_Liste (T : Tab) return T_Liste is
      L : T_Liste := new T_Cellule;
   begin
      if T'Length = 0 then return null; end if;
      L.Valeur := T (T'First);
      declare
     C : constant T_Liste := L;
      begin
     for I in T'First + 1 .. T'Last loop
        L.suivant := new T_Cellule;
        L := L.Suivant;
        L.Valeur := T (I);
     end loop;
     return C;
      end;
   end Create_Liste;

   procedure SELECTION
      (Liste : in out T_Liste; 
       Less_Than : access function 
          (ELEMENT, Element2 : in T_ELEMENT) return BOOLEAN) 
   is
      Head : constant T_Liste := new T_Cellule '(Suivant => Liste, others => <>); -- meant as something to affect to "Liste" at last.
      Iter : T_Liste := Head; -- that which will iterate through the list each time a new element needs to be tested against the ones before it.
      Pred : T_Liste := Liste; -- that before the "current", which has the element to be inserted. Needed so as to sew properly elements with each other.
   begin
      Liste := Liste.Suivant;
      while Liste /= null loop
         declare
            Suivant : constant T_Liste := Liste.Suivant; -- so as to record the next element to be inserted.
         begin
            Iter := Head;
            while Iter /= Liste loop
               if Less_Than (Liste.Valeur, Iter.Suivant.Valeur) then
                  Pred.Suivant := Liste.Suivant;
                  Liste.Suivant := Iter.Suivant;
                  Iter.Suivant := Liste;
               end if;
               Iter := Iter.Suivant;
            end loop;
            Liste := Suivant;
            Iter := Head;
         end;
      end loop;
      Liste := Head.Suivant; -- Liste is set to the beginning of the newly sewn list.
   end SELECTION;

   LISTE_SELECTION : T_LISTE := Create_Liste ((5, 2, 16, 20, 8));
   function Less_Than (A, B : Natural) return Boolean is ( A < B);

   Iter : T_Liste;
begin
   for I in 1 .. 2 loop
      ITER := LISTE_SELECTION;
      while Iter /= null loop
         Put (Iter.VALEUR'IMG & ' ');
         Iter := Iter.Suivant;
      end loop;
      New_Line (2);
      SELECTION (LISTE_SELECTION, Less_Than'ACCESS);
   end loop;
end Main;

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

您的问题是(此列表可能不完整):

    <bean id="mongoURI" class="com.mongodb.MongoClientURI">
        <constructor-arg name="uri" value="${MONGO_URI}" />
    </bean>

    <bean id="mongoCLIENT" class="com.mongodb.MongoClient">
        <constructor-arg ref="mongoURI" />
    </bean>
    <bean id="mongoDao" class="com.dao.MongoDaoImpl">
        <constructor-arg ref="mongoCLIENT" />
    </bean>

两者都永远不会更新,所以

  • Head : constant T_Liste := new T_Cellule '(Suivant => Liste, others => <>); -- meant as something to affect to "Liste" at last. Pred : T_Liste := Liste; -- that before the "current", which has the element to be inserted. Needed so as to sew properly elements with each other. 将始终指向节点Head.Suivant(对于更复杂的输入,这将失败),
  • 5将始终指向节点Pred(因此,只要您对5进行测试,它就会更新Less_Than,无论手头的节点如何)

编辑: 我会做这样的事情(不需要5.Suivant):

Pred