S
S
sasha_zaitsev2017-04-17 16:41:16
Algorithms
sasha_zaitsev, 2017-04-17 16:41:16

How to change the bubble method?

Write a variant of the bubble method that terminates if there were no permutations at the next step of the outer loop.
Where is the mistake?

const n=10;
type s = array[1..n] of integer;
var mas: s;
var i,j,t,k: integer;
begin
for i:=1 to n do mas[i]:=random(2);
for i:=1 to n do write(mas[i],' ');
writeln();
k:=0;
for i:=1 to n-1 do
   begin
   for j:=n-1 downto i do
    if mas[j+1]<mas[j] then
      begin
      t:=mas[j+1];
      mas[j+1]:=mas[j];
      mas[j]:=t;
      k:=k+1;
      end;
   if k=0 then break;
   k:=0;
   end;
for i:=1 to n do write(mas[i],' ');
end.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tsarevfs, 2017-04-17
@tsarevfs

10 element will never be checked with a neighbor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question