How do you use sorting bubbles with pointers in C ++?

So here is what I still have:

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;
    }
}

Basically, I try to read, no matter how many user numbers you enter, and then sort them in ascending order. To catch, I have to use pointers, and I never understood them. This code above works for 3 numbers, however, it adds even more reasons not to sort them ... I tried the shooting problem as much as I could, but without any knowledge in the pointers. I do not know what I am looking for.

Thanks for the help!

+3
source share
2 answers

The problem could be here:

    if(*(testScores+i) > *(testScores+j+1)) 

You meant:

        if(*(testScores+j) > *(testScores+j+1)) 

(note i replaced by j).

btw, Bubble sort, , . .

+3

Bubble , ().

, , , .

.

+1

Source: https://habr.com/ru/post/1731706/


All Articles