Answer the question
In order to leave comments, you need to log in
Would you call this algorithm bubble sort?
In general, this code
const
maxn = 100;
var
a: array[1..maxn] of longint;
i, j, r, n: longint;
begin
readln(n);
for i:= 1 to n do
read(a[i]);
for i:= 1 to n-1 do
for j:= i+1 to n do
if a[i] > a[j] then
begin
r:= a[i]; a[i]:= a[j]; a[j]:= r;
end;
for i:= 1 to n do
write(a[i], ' ');
writeln;
end.
Answer the question
In order to leave comments, you need to log in
The basic principle of bubble sort is the exchange of two nearby elements of an array, going through all the elements.
What do you think?
UPD1:
const
maxn = 100;
var
a: array[1..maxn] of longint;
i, j, r, n: longint;
begin
readln(n);
for i:= 1 to n do
read(a[i]);
for i:= 1 to n-1 do
for j:= i+1 to n do
if a[j] > a[j-1] then
begin
r:= a[j]; a[j]:= a[j-1]; a[j-1]:= r;
end;
for i:= 1 to n do
write(a[i], ' ');
writeln;
end.
const
maxn = 100;
var
a: array[1..maxn] of longint ;
i, j, r,n : longint;
begin
readln(n);
for i:= 1 to n do
read(a[i]);
for i:= 1 to n do
for j:= 1 to n-i do
if a[j] > a[j+1] then
begin
r:= a[j]; a[j]:= a[j+1]; a[j+1]:= r;
end;
for i:= 1 to n do
write(a[i], ' ');
writeln;
end.
If the question is, "Would you call this algorithm bubble sort?", then the answer is certainly that the code shown initially is a variant of bubble sort, the worst sorting algorithm known.
Small variations in the implementation do not affect the name of the method.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question