Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C code bubble sorting sometimes does not give correct output

I am trying to sort numbers using the bubble sorting method. However, sometimes the output returns some incorrect answers.

Appreciate someone can help how to fix this issue. the C code and incorrect output as below

typedef struct
{
    char pname[2];
    int ptotime;
} data;

int main()
{
    int totalwait = 0;
    data name[] = {{'A', .ptotime = 4},
                   {'B', .ptotime = 6},
                   {'C', .ptotime = 3},
                   {'D', .ptotime = 7},
                   {'E', .ptotime = 2}};

    printf("Process Name\t Process Time \n");

    for (int j = 0; j < 5; j++)
    {
        printf("\t%s\t\t\t\t%d\n", name[j].pname, name[j].ptotime);
    }

    //Shortest job first (SJF) scheduling
    printf("\nShortest job first (SJF) scheduling \n");

    int swapped, temp;

    while (1)
    {
        swapped = 0;

        for (int x = 0; x < 5; x++)
        {
            if (name[x].ptotime >= name[x + 1].ptotime)
            {
                temp = name[x].ptotime;
                name[x].ptotime = name[x + 1].ptotime;
                name[x + 1].ptotime = temp;
                swapped = 1;
                char temp2[2];
                strcpy(temp2, name[x].pname);
                strcpy(name[x].pname, name[x + 1].pname);
                stpcpy(name[x + 1].pname, temp2);
            }
        }

        if (swapped == 0)
        {
            break;
        }
    }
    printf("Process Name\t Process Time \n");

    for (int j = 0; j < 5; j++)
    {
        printf("\t%s\t\t\t\t%d\n", name[j].pname, name[j].ptotime);
    }

    return 0;
}

Output

Process Name     Process Time
        A                               4
        B                               6
        C                               3
        D                               7
        E                               2

Shortest job first (SJF) scheduling
Process Name     Process Time
                                        0
        E                               2
        C                               3
        A                               4
        B                               6

Expected output

Process Name     Process Time
        A                               4
        B                               6
        C                               3
        D                               7
        E                               2

Shortest job first (SJF) scheduling
Process Name     Process Time
        E                               2
        C                               3
        A                               4
        B                               6
        D                               7
like image 681
Safras Iqbal Avatar asked Sep 26 '26 16:09

Safras Iqbal


2 Answers

For starters do not use magic numbers as 5. Use named constants.

The array name is initialized incorrectly. You are using character literals to initialize the data member pname of a character array type without enclosing character literals in braces.

data name[] = {{'A', .ptotime = 4},
                ^^^
//...

in this loop

    for (int x = 0; x < 5; x++)
    {
        if (name[x].ptotime >= name[x + 1].ptotime)
                                    ^^^^^
        //...

there is an access beyond the array. So the program has undefined behavior.

Use local variables as for example the variable swap in the shortest scope where they are used.

To swap elements of the array name there is no need to swap each data member of each element of the array. You can swap whole objects.

Here is a demonstrative program.

#include <stdio.h>

typedef struct
{
    char pname[2];
    int ptotime;
} data;


int main( void ) 
{ 
    data name[] = 
    {
        { "A", .ptotime = 4 },
        { "B", .ptotime = 6 },
        { "C", .ptotime = 3 },
        { "D", .ptotime = 7 },
        { "E", .ptotime = 2 }
    };
    const size_t N = sizeof( name ) / sizeof( *name );

    printf("Process Name\t Process Time \n");

    for ( size_t i = 0; i < N; i++ ) 
    {
        printf( "\t%s\t\t\t\t%d\n", name[i].pname, name[i].ptotime );
    }

    putchar( '\n' );

    //Shortest job first (SJF) scheduling

    printf("\nShortest job first (SJF) scheduling \n");

    for ( int swapped = 1; swapped; )
    {
        swapped = 0;

        for ( int i = 1; i < N; i++ ) 
        {
            if ( name[i].ptotime < name[i-1].ptotime )
            {
                data tmp = name[i];
                name[i] = name[i-1];
                name[i-1] = tmp;

                swapped = 1;
            }
        }
    }

    printf("Process Name\t Process Time \n");

    for ( size_t i = 0; i < N; i++ ) 
    {
        printf( "\t%s\t\t\t\t%d\n", name[i].pname, name[i].ptotime );
    }

    return 0;
}

Its output is

Process Name     Process Time 
    A               4
    B               6
    C               3
    D               7
    E               2


Shortest job first (SJF) scheduling 
Process Name     Process Time 
    E               2
    C               3
    A               4
    B               6
    D               7
like image 166
Vlad from Moscow Avatar answered Sep 28 '26 08:09

Vlad from Moscow


Change:

for(int x = 0; x < 5; x++) {

into:

for(int x = 0; x < 4; x++) {

When x=4, the code compares name[4] with name[5], but name[5] is out of bounds (the only valid elements are name[0] ... name[4]).

like image 29
Bruno Rijsman Avatar answered Sep 28 '26 08:09

Bruno Rijsman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!