2

I've got a task to code some sorting function by passing pointers. Unfortunately, pointers are just one of those concepts my brain doesn't seem to comprehend.

Here's the call:

int size = 10000;
int* data = new int[size]; 
//omitted code that populates array for the sake of space
selectionSort(data, data+size); 

And here's my incorrect attempt at the function:

void selectionSort(int* first, int* last) { 
for (int* i = first; i < last-1; i++) {
    int* min = i;
    for (int* j = i+1; j < last; j++) {
        if (j < min) {
            min = j; 
        }
        int* temp = i; 
        i = min; 
        min = temp; 
    }
}

}

Basically, I'm having trouble figuring out what happens when I'm comparing one pointer with another, or adjusting a pointer. Is it adjusting/comparing the value it's pointing too or is it comparing the actual pointers themselves?

Any help is appreciated. Cheers.

1
  • 1
    You are comparing the address the pointer holds. If you want the value it pointers to, you need to defer it eg. *min = *j. Also you want i < last since last is one position pass the last element -- usual convention followed by stl. Commented Oct 25, 2013 at 1:48

3 Answers 3

4

Pointers are sometimes a difficult concept to grok. Perhaps it will help to think of them as a reference to a value, rather than the value itself (it's an address of the mailbox, not the contents of the mailbox).

In your code 'int* min = i;' sets min to the same address (reference) as 'i'. So later in your 'if' statement 'if (j < min)' you are comparing references, not values. You need to "dereference" your pointer to get at the values, like this: 'if (*j < *min)'.

Sign up to request clarification or add additional context in comments.

Comments

0

In pointers if,

int* i; //i holds actual physical memory address & i* holds value at that address.

Now, in your selectionSort functions if condition you are comparing actual memory addresses and not values. See example here.

Comments

-1

You should never really be put in a position that you need to create your own sort algorithm. Instead, you should always try to utilize existing collection class, eg set, which will sort for you. That being said, if your goal is to better understand pointers, then as greatwolf commented, you shouldn't be sorting pointers, but what they are pointing to! In your case integers.

3 Comments

Implementing sorting algorithms is a great way to learn the hard parts of programming.
@DominicMcDonnel, was there any part to my response that indicates implementing sorting algorithms is NOT a great way to learn the hard parts of programming? Your missing the point and you didn't read my response. If the poster was legitimately looking to solve a real problem, they would typically not have to write there own sorting algorithm as the work has been done many times over and is available to them via STL. Not to mention my response "if your goal is to better understand pointers..."!!
I wasn't the downvoter. You didn't specifically say that it wasn't, but then you didn't specifically say it was. I thought it was a useful point so I added it as a comment. You can do sorting without using pointers as well so it doesn't necessarily fall under that part of your answer either.

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.