0

As the title says I have two algorithm for bubble sorting but the one I wrote is not working in all cases.

This is the code that works in every case

procedure Sort( t: in out Item_array) is
   Finished : Boolean;
   tmp: Item;
begin
loop
  Finished := True;
  for J in t'First..Index'Pred(t'Last) loop
     if t(J) < t(Index'Succ(J)) then
        Finished := False;
        tmp := t(Index'Succ(J));
        t(Index'Succ(J)) := t(J);
        t(J) := tmp;
     end if;
  end loop;
exit when Finished;
end loop;
end Sort;

And here is my code which is not working with the following array (2,4,3,2,2,5). I assume It is because of multiple '2's.

procedure Sort( t: in out Item_array) is
   I : Index := t'Last;
   CS: Index := t'First;
   tmp: Item;
begin
   while I >= Index'Succ(t'First) loop
      for J in t'First..Index'Pred(I) loop
         if t(J) < t(Index'Succ(J)) then
            tmp := t(Index'Succ(J));
            t(Index'Succ(J)) := t(j);
            t(j) := tmp;
            CS := J;
         end if;
      end loop;
      I := CS;
   end loop;
end Sort;

In both cases the "<" operator is a function template parameter, not a regular "less" or "more" operator. And this is how I call the function.

function Asc(A,B : in Integer) return Boolean is (A > B); // Sorting function

procedure Sort_Asc is new Sort(Item=>Integer, 
                                Index=>Positive, 
                                Item_Array=>Int_Array,
                                "<"=>Asc);

x : Int_Array := (2,4,3,2,2,5);
Sort_Asc(x);

If I use the second algorithm, It'll stuck in an infinite loop. //Please don't mind my english.

5
  • 1
    Is it possible that the infinite loop is due to the CS variable not changing once the array is sorted, and therefore I never changes? You might consider changing that line I := CS to I := Succ(I). Or should that be Pred(I)? Whichever way you're going. (It's been too many years since I even looked at Ada.) Mar 23, 2017 at 20:51
  • @JimMischel You might be right! But I declared as CS : Index := t'First ,so if the array is all sorted I must be t'First therefore the while loop should broke.
    – Bako
    Mar 23, 2017 at 20:57
  • 1
    Can you think of a better name for CS? One which explains what it means, what it’s for. And I, come to that. Mar 23, 2017 at 23:07
  • Seems like it'd be trivial to give my suggestion a try. If it works, then you know that the problem is with the way you're modifying I in that loop. I think you're trying to use that CS variable to do some kind of optimization, although as @SimonWright points out, it's rather difficult to say for sure. Mar 23, 2017 at 23:42
  • And, by the way, if I is equal to t'First, then it looks like your loop won't break because your condition is >=, not >. But then, I could be misunderstanding the Ada syntax. Mar 23, 2017 at 23:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.