0

I'm trying to sort an array of pointers according to the values in an array, leaving the original dataset alone. However, when I output the pointer array and then the data array, they both have the same sorted values. How can I sort the array of pointers without changing data[]?

I think what's happening is that the values that arrptr[] is pointing to are being sorted and in turn the data[] array is somehow being messed up. I tried using a pointer to a pointer but only got errors (int** to int* conversion).

For reference, this is how I assigned the pointer array to the data array:

 for (int i = 0; i < size; i++)
 {
     ReadingFile >> num1;
         
     data[i] = num1;

     ptrarr[i] = &data[i];
 }

These are the display functions that output the same exact sorted result:

static void DisplayArray(int* ptrarr[], int size)
{
    cout << "The Pointer Array of sorted pointer elements:\n";
    
    for (int i = 0; i < size; i++)
    {
        cout << *ptrarr[i] << "     ";   //gives same result as DisplayData
    }
    
    cout << endl;

    return;
}

static void DisplayData(int data[], int size)
{
    cout << "Data Array elements are:\n";

    for (int i = 0; i < size; i++)
    {
        cout << data[i] << "     ";
    }

    cout << endl;

    return;
}

Here are my sorting functions:


static void SwapIntPtr(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;

    return;

}
static void Sorting(int* ptrarr[], int size)
{

        for (int i = 0; i < size - 1; i++)
        {
            for (int j = 0; j < size - i - 1; j++)
            {
                if (*ptrarr[j] > *ptrarr[j + 1])
                {
                    SwapIntPtr(ptrarr[j], ptrarr[j + 1]);
                }
            }
        }
        
        return;

}

It's hard for me to see the issue as everything looks like it should work. However, I'm hoping a fresh set of eyes could help me see the problem.

7
  • 1
    Your swap function is not changing the POINTERS. It's changing the things the pointers POINT at. Your Swap function needs to accept int ** a and int ** b. Commented Feb 10, 2024 at 18:30
  • You can observe the flaw, here, very easily. Just ask yourself: what is your swap function swapping. It gets a pair of pointers to ints, right? And it's swapping the ints, right? Well, if your goal is to sort the pointers, which involves swapping the pointers, then swapping the ints, instead would be described by Mr. Spock as "highly illogical". Commented Feb 10, 2024 at 18:32
  • 1
    Is this a school or similar assignment or exercise? Because if it's not then you should just use std::sort with a suitable lambda for the comprison function. Commented Feb 10, 2024 at 18:39
  • Just one line int temp = *a; shows that you are sorting integers, not pointers. Commented Feb 10, 2024 at 18:49
  • 1
    Why are you even sorting int*? Sorting ints directly would have been more easy, there is no need for pointers here. Then you use "C" style arrays, where std::span (C++20) is probably the better choice, adn finally as said just use std::sort. If someone is teaching you C++ it looks like that someone is teaching you a somewhat dated version of C++. Note for arrays it is more idiomatic to use std::vector<int>. Commented Feb 10, 2024 at 19:47

2 Answers 2

1

SwapIntPtr swaps the values, not the pointers. Just use std::swap or if you really need to implement your own swap:

static void SwapIntPtr(int*& a, int*& b)
{
  int* temp = a;
  a = b;
  b = temp;
}

Note that the pointers need to be passed by reference in order for the swap function to be able to modify them.

It would be simpler to use std::sort:

std::sort(ptrarr, ptrarr + size, [](int* a, int* b) { return *a < *b; });
Sign up to request clarification or add additional context in comments.

Comments

0

As I said, you're sorting the things the pointers point at, not the pointers themselves. This works:

#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

static void DisplayArray(int* ptrarr[], int size)
{
    cout << "The Pointer Array of sorted pointer elements:\n";
    
    for (int i = 0; i < size; i++)
    {
        cout << *ptrarr[i] << "     ";   //gives same result as DisplayData
    }
    
    cout << endl;

    return;
}

static void DisplayData(int data[], int size)
{
    cout << "Data Array elements are:\n";

    for (int i = 0; i < size; i++)
    {
        cout << data[i] << "     ";
    }

    cout << endl;

    return;
}


static void SwapIntPtr(int**a, int**b)
{
    int* temp = *a;
    *a = *b;
    *b = temp;
    return;
}

static void Sorting(int* ptrarr[], int size)
{

        for (int i = 0; i < size - 1; i++)
        {
            for (int j = 0; j < size - i - 1; j++)
            {
                if (*ptrarr[j] > *ptrarr[j + 1])
                {
                    SwapIntPtr(&ptrarr[j], &ptrarr[j + 1]);
                }
            }
        }
        
        return;

}

int main()
{
    int size=9;
    int data[] = { 9, 3, 6, 2, 8, 17, 42, 1, 7 };
    int * ptrarr[9];

    for (int i = 0; i < size; i++)
    {
        ptrarr[i] = &data[i];
    }

    Sorting(ptrarr, size);
    DisplayData( data, size );
    DisplayArray( ptrarr, size );
}

However, you don't even need that much. The standard library includes a perfectly functional sort routine that can handle your array:

    sort( begin(ptrarr), end(ptrarr), [](int * p1, int * p2){return *p1 < *p2;});

Output for both:

Data Array elements are:
9     3     6     2     8     17     42     1     7     
The Pointer Array of sorted pointer elements:
1     2     3     6     7     8     9     17     42     

1 Comment

It also has a perfectly reasonable swap function if for some reason you wanted to do your own sorting algo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.