0

I am currently trying to sort an array of pointers using selection sort, but with no success. I need to sort an array of int values with only pointers. I have already stored values in the array, and only thing left to do is to sort these values.

Here is my code:

void fill_rand_ver2(int *pointer, int size)
{
int randNbr;
int *i;

for(i = pointer; i < (pointer + size); i++)
{
    randNbr = (rand()%10)+1;
    *i = randNbr;
}
}

// sort array with help of selection sort using pointer
void sort_ver2(int *pointer, int size)
{
int *i, *j, swap;

for(i = pointer; i < (pointer + size); i++)
{
    for(j = i + 1; j < (pointer + size); j++)
    {
        if(*j < *i)
        {
            swap = *i;
            *i = *j;
            *j = swap;
        }
    }
}
}

// print out both arrays using pointer
void print_values_ver2(char *string, int *pointer, int size)
{
int *i;

printf("%s", string);
printf("\n");

for(i = pointer; i < (pointer + size); i++)
{
    printf("%d,", *i);
}
printf("\n\n");
}

When I run the program and print data, the entered values are not sorted and remain at the same place. I would appreciate any help given.

3
  • 1
    Hm, I believe you want to sort by value, not address, so it should be if(*j < *i) instead of if(j < i) Commented Oct 22, 2013 at 10:29
  • @KBart post that as an answer and you have my up-vote. And note to the OP, the reason nothing is done is simple: j is always greater than i). Also (j < i+size) is not correct in the inner for-condition. It will send j out of range and into undefined behavior if the pointer sequence is indeed only size elements wide. Commented Oct 22, 2013 at 10:31
  • @WhozCraig Yes, thanks, I'll edit my answer to note about incorrect range too. Commented Oct 22, 2013 at 10:35

1 Answer 1

1

I believe you want to sort by value, not by address, so it should be:

void sort_ver2(int *pointer, int size)
{
    int *i, *j, swap;
    int *end = NULL;

    if(size < 2 || pointer == NULL)
        return;

    end = pointer + size - 1;

    for(i = pointer; i < end; i++)
    {
        for(j = i + 1; j <= end; j++)
        {
            if(*j < *i)
            {
                swap = *i;
                *i = *j;
                *j = swap;
            }
        }
    }
}

Even if you would like to sort by address, your if statement is incorrect, as j is always greater than i in your code.

EDIT: added end variable to address issue with incorrect range of the inner for loop. Credits to @WhozCraig.

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

5 Comments

I want to sort by value, and i tried your method. But it still doesnt seem to sort. How do you suggest I change my if statement?
What you mean by "doesn't sort"? Do you get runtime error? If no, then show your main(), might be a problem with the rest of your code.
This almost worked. Except for one thing, the first output is not sorted, it writes something like -858993460. Run-Time Check Failure #2 - Stack around the variable 'array' was corrupted.
@Wooolie, it depends on size parameter you pass, I guess it's a number of elements you use, so check it now.
Yes, instead of j <= end, I used only j < end. And it worked fine. Thank you !

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.