Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Wrong output of bubble sort algorithm in Pascal

Recently I have been trying to create a bubble sort algorithm in Pascal language, however after the program is run, the output turned out to be only 0s and 1s. I wonder what have I done wrong.

program BubbleSort;

const n = 9;
var
    arr : array [0..n] of integer;
    num, a, b, i, j, temp : integer;
begin
    temp := 0;
    for a := 0 to n do
    begin
        writeln('Please enter the ', a, '-th element in the array:');
        readln(arr[a]);
    end;
    for i := 0 to (n - 1) do
    begin
        for j := 0 to (n - i - 1) do
        begin
            if (arr[j] > arr[j + 1]) then
            begin
                arr[j] := temp;
                arr[j] := arr[j + 1];
                arr[j + 1] := temp;
            end;
        end;
    end;
    writeln('The sorted array is as follows:');
    for b := 0 to n do
    begin
        write(arr[b], ' ');
    end;
end.

Since I am familiar with C# and Java, I tried my best to declare the array to start from index 0, and that might be the cause of the problem. I have no idea.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem lies in the swapping of the elements in the array. You’re not correctly swapping the values of arr[j] and arr[j + 1]. Instead, you’re overwriting arr[j] with temp (which is 0) and then assigning arr[j + 1] to temp, effectively losing the original value of arr[j].

Here’s the corrected version of the swapping code in your bubble sort algorithm:

if (arr[j] > arr[j + 1]) then
begin
    temp := arr[j];
    arr[j] := arr[j + 1];
    arr[j + 1] := temp;
end;

With this change, your complete Pascal code for the bubble sort should look like:

program BinarySearch;

const n = 9;
var
    arr : array [0..n] of integer;
    a, b, i, j, temp : integer;
begin
    temp := 0;
    for a := 0 to n do
    begin
        writeln('Please enter the ', a, '-th element in the array:');
        readln(arr[a]);
    end;
    for i := 0 to (n - 1) do
    begin
        for j := 0 to (n - i - 1) do
        begin
            if (arr[j] > arr[j + 1]) then
            begin
                temp := arr[j];
                arr[j] := arr[j + 1];
                arr[j + 1] := temp;
            end;
        end;
    end;
    writeln('The sorted array is as follows:');
    for b := 0 to n do
    begin
        write(arr[b], ' ');
    end;
end.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading