Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C代码气泡排序有时不能给出正确的输出_C_Sorting_Struct_Bubble Sort - Fatal编程技术网

C代码气泡排序有时不能给出正确的输出

C代码气泡排序有时不能给出正确的输出,c,sorting,struct,bubble-sort,C,Sorting,Struct,Bubble Sort,我试图用气泡排序法对数字进行排序。但是,有时输出返回一些不正确的答案 感谢有人能帮助解决这个问题。C代码和不正确的输出如下所示 typedef struct { char pname[2]; int ptotime; } data; int main() { int totalwait = 0; data name[] = {{'A', .ptotime = 4}, {'B', .ptotime = 6},

我试图用气泡排序法对数字进行排序。但是,有时输出返回一些不正确的答案

感谢有人能帮助解决这个问题。C代码和不正确的输出如下所示

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;
}
预期产出

进程名称进程时间
A 4
B 6
C3
D 7
E2
最短作业优先(SJF)调度
进程名称进程时间
E2
C3
A 4
B 6
D 7

这里有一个问题:

for (int x = 0; x < 5; x++) {
      if (name[x].ptotime >= name[x + 1].ptotime) {
for(int x=0;x<5;x++){
如果(名称[x].ptotime>=名称[x+1].ptotime){
x
可以接受的最大值是4。但是
name[x+1]
将访问数组末尾之外的一个元素,该数组只有5个元素。访问数组超出边界将导致未定义的行为,所有下注都将关闭

不过,可能还有更多问题。

更改:

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

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

x=4
时,代码会将
name[4]
name[5]
进行比较,但是
name[5]
超出范围(唯一有效的元素是
name[0]
name[4]
)。

在交换周期中,此代码片段:

for(int x = 0; x < 5; x++)
这样,最后一个周期将把
名称[3+1]
复制到
名称[3]
,从第5个元素复制到第4个元素,这是预期的行为


对于初学者,不要将幻数用作
5
。请使用命名常量

数组名称初始化不正确。您正在使用字符文本初始化字符数组类型的数据成员
pname
,而不将字符文本括在大括号中

data name[] = {{'A', .ptotime = 4},
                ^^^
//...
在这个循环中

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

当您使用调试器运行程序时,您看到了什么?这就是调试器的用途。如果您不知道如何使用调试器,这是一个很好的机会,可以学习如何使用调试器一次运行一行程序,监视所有变量及其值的变化,并分析程序的逻辑执行流。了解使用调试器是每个C/C++开发人员的必备技能,没有例外。在调试器的帮助下,您应该能够快速找到此程序和您编写的所有未来程序中的所有错误,而无需向任何人寻求帮助。您的数组有5个元素。当代码访问不存在的第6个元素时会发生什么?请编辑您的问题是将实际输出和预期输出包括为文本,而不是图片。冒泡排序应该是这样。但是,您的比较是
=
。您应该删除
=
部分。我认为初始化
字符[2]会导致未定义的行为
'A'
一起使用。如何确保第二个字符为空?我将使用
'A“
作为初始值设定项。与结构的其他实例类似。感谢您的及时响应。这解决了错误。干杯。感谢您的及时响应。这解决了错误。干杯。一个完整的信息性答案,对我帮助很大,因为我是一个试图学习C/C++的学生。非常感谢您的时间。谢谢
data name[] = {{'A', .ptotime = 4},
                ^^^
//...
    for (int x = 0; x < 5; x++)
    {
        if (name[x].ptotime >= name[x + 1].ptotime)
                                    ^^^^^
        //...
#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;
}
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