Hi, I have been searching in google for an algorithm for sort a dynamic array. I must said that the information in the Internet for sort dynamic array is very limited. However, I found bubble sort for dynamic array, but it don't wanna work. It is inside a class definition and I'm calling it from a function inside the same class.

Here is how a'm calling it: arrSort (arrayOfList, listSize);

Declaration in Public:
void arrSort(int*, int);

void file::arrSort(int* pDonate[], int s)
{
    bool swap;
    int* temp;
    do
    {
        swap = false;
        for (int count = 0; count < (s-1); count++)
        {
            if ((*pDonate[count]) > (*pDonate[count+1]))
            {
                temp = pDonate[count];
                pDonate[count] = pDonate[count+1];
                pDonate[count+1] = temp;
            }
            swap = true;
        }
    }
    while(swap);
}

Here is the error:
C:\Users\xxx\Desktop\file.h prototype for `void file::arrSort(int**, int)' does not match any in class `file'
C:\Users\xxx\Desktop\file.h void file::arrSort(int*, int)
I'm using Dev C++.

Any suggestion will be appreciate.

Dani AI

Generated

Duoas is spot on about the type mismatch. In a function parameter list, int a[] is the same as int* a, but int* a[] is the same as int** a. Your header declares void arrSort(int*, int); while the definition uses int* pDonate[] (i.e., int**). That is why the compiler says the prototype does not match. Pick one and make both places identical.

Also, a small logic bug: in your bubble sort you set swap = true; on every pass of the inner loop. That makes the do { ... } while (swap); loop never terminate. Set it to true only when you actually swap.

If you are sorting an array of ints:

// file.h
void arrSort(int* a, int n);

// file.cpp
#include <algorithm>

void file::arrSort(int* a, int n) {
    bool swapped;
    do {
        swapped = false;
        for (int i = 0; i < n - 1; ++i) {
            if (a[i] > a[i + 1]) {
                std::swap(a[i], a[i + 1]);
                swapped = true;
            }
        }
    } while (swapped);
}

If you are sorting an array of pointers to int by the pointed-to values, then both declaration and definition must use int**, and you compare dereferenced values:

// file.h
void arrSort(int** p, int n);

// file.cpp
#include <algorithm>

void file::arrSort(int** p, int n) {
    bool swapped;
    do {
        swapped = false;
        for (int i = 0; i < n - 1; ++i) {
            if (*p[i] > *p[i + 1]) {
                std::swap(p[i], p[i + 1]); // swaps pointers, not the ints
                swapped = true;
            }
        }
    } while (swapped);
}

Last note to kako13: an array is not an address. Arrays decay to a pointer to their first element when passed to functions, which is why int a[] becomes int* a in parameters, but arrays and pointers are still distinct types in C++.

Recommended Answers

All 3 Replies

You've got a type problem. Always make sure you are handling the same type of things.

An int *foo; is the same as an int foo[]; But it is very different from an int *foo[]; (which is the same as an int **foo; )

Hope this helps.

Hi, if I'm right my array is int* arrayName, while the algorithm is for sort an array of type int* arrayName[]? I'm a little confused with that, then I don't be will be able to sort the dynamic array with that algorithm?

Thanks...

You've got a type problem. Always make sure you are handling the same type of things.

An int *foo; is the same as an int foo[]; But it is very different from an int *foo[]; (which is the same as an int **foo; )

Hope this helps.

Solved! Now I understand an array is an address. For that reason I don't need to have int* x [], because only with int x [] I'm pointing to a direction.

Hi, if I'm right my array is int* arrayName, while the algorithm is for sort an array of type int* arrayName[]? I'm a little confused with that, then I don't be will be able to sort the dynamic array with that algorithm?

Thanks...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.