A
A
Aitym Ramazanov2015-11-01 11:33:11
Programming
Aitym Ramazanov, 2015-11-01 11:33:11

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

2 answer(s)
A
Alexey S., 2015-11-01
@iTeam91

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.

UPD2: but this is more correct:
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.

O
Oleg Tsilyurik, 2015-11-01
@Olej

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 question

Ask a Question

731 491 924 answers to any question