Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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++;?_C++_Arrays_Pointers_Bubble Sort - Fatal编程技术网

C++ 如何在c++;?

C++ 如何在c++;?,c++,arrays,pointers,bubble-sort,C++,Arrays,Pointers,Bubble Sort,以下是我到目前为止的情况: void sortArray(int amountOfScores, int* testScores) { for(int i = 0; i < amountOfScores; i++) { for(int j = 0; j < amountOfScores-1; j++) { if(*(testScores+i) > *(testScores+j+1))

以下是我到目前为止的情况:

void sortArray(int amountOfScores, int* testScores)
{
    for(int i = 0; i < amountOfScores; i++)
    {
        for(int j = 0; j < amountOfScores-1; j++)
        {
            if(*(testScores+i) > *(testScores+j+1))
            {
                int temp = *(testScores+j);
                *(testScores+j) = *(testScores+j+1);
                *(testScores+j+1) = temp;
            }
        }
    }       
    for(int i = 0; i < amountOfScores; i++)
    {
        cout << *(testScores+i) << endl;
    }
}
void sortArray(int amountOfScores,int*testScores)
{
对于(int i=0;i*(测试分数+j+1))
{
int temp=*(测试分数+j);
*(测试分数+j)=*(测试分数+j+1);
*(测试分数+j+1)=温度;
}
}
}       
对于(int i=0;i你的问题可能在这里:

    if(*(testScores+i) > *(testScores+j+1)) 
你是说:

        if(*(testScores+j) > *(testScores+j+1)) 
(注i替换为j)


顺便说一句,在冒泡排序中,如果没有交换,您应该中断。这在某些情况下会导致速度加快。

无论您谈论的是数组还是链表(指针),冒泡排序的工作原理都是一样的

唯一的问题是,不是交换数组中两个相邻项的位置,而是在两个相邻列表元素之间交换指针值


算法是相同的。

testScores被声明为指向int的指针,如果您想使用int*指针进行排序,则需要将int**传递到排序函数中。
int**testScores
int*testScores[]
,我认为第二种形式更清晰。有一条建议:阅读一些关于指针的教程以及它们是什么以及如何与数组一起使用它们。这会让你的生活更轻松!这是家庭作业吗?如果是,用“家庭作业”标签标记它。如果不是,为什么要使用冒泡排序?@John Knoler:那不是真的。如果是,他只需要传递一个指针试图改变指针指向的位置,但他不是。只要他只想交换指向的项目,一个简单的指针就可以了。@Tony我已经阅读了本章和一些网站,只是出于某种原因没有掌握它们。@Mark Byers,因为我仍然只是在学习指针,一旦我对它们更熟悉,我就不会了Don’不要使用简单的排序形式,比如冒泡排序。OP的问题或代码中没有任何东西表明他在处理链表。你的回答假设我注意到了他的代码。坏习惯。是的,所以我只是把头撞在桌子上。的确,我的意思是j,而不是我,我认为这将是一件愚蠢的简单事情。比杰夫:是啊,你的指针使用似乎还不错。只有你的打字技能需要提高:)