1

This program is supposed to take an array, and sort it from lowest to highest value. My program won't sort any values though. I believe the error is in the selectionSort. The values i and j are present in the function, I printed them out inside the function but they are not passed into the swap function. I tried making i and j pointers but it didn't work. I just have no clue on what to do next. Any help would be appreciated.

#include <stdio.h>
#define N 5

void selectionSort(int *a, int n);
int *findLargest(int *a, int n);
void swap(int *p, int *q);

int main(void)
{
    int i;
    int a[N];

    printf("Enter %d numbers: ", N);
    for (i = 0; i < N; i++) {
        scanf("%d", &a[i]);
     }

    selectionSort(a, N);

    printf("In sorted order:");
    for (i = 0; i < N; i++) {
        printf(" %d", a[i]);
    }
    printf("\n");

    return 0;
}

void selectionSort(int *a, int n)
{
    int *p = a;
    int i;
    int j;

    if (n == 1) {
        return;
    }

    i = *(p+n-1);
    j = *findLargest(a, n);
    swap(&i, &j);

    selectionSort(a, n - 1);
 }

int *findLargest(int *a, int n)
{
    int *p;
    int *p_max = a;

     for(p = a + 1; p < a + n - 1; p++) {
        if ( *p > *p_max)
            p_max = p;
     }
    return p_max;
 }

void swap(int *p, int *q)
{
     int temp = *(p-1);
    *(p-1) = *q;
    *q = temp;
}
2
  • If you haven't tried to use a debugger, now is the perfect time to learn how to use one. With it you can step through code, line by line, go into or jump over functions calls, and monitor variables and their values. Build a version of your program with debug information, and run in a debugger to help you find the problem. Knowing how to use a debugger is very important if you want to be at all serious with programming, as it's a big part of the bigger "programming" picture. Commented Oct 4, 2015 at 12:46
  • Oh, and for any pointer or array a and integer i, the expression *(a + i) is equivalent to a[i]. Besides being a little less to write, it also in most cases shows the intents better making the code easier to read, understand and maintain. Commented Oct 4, 2015 at 12:55

1 Answer 1

1

The problem is in your call of swap: you swap the content of two local variables

int i;
int j;
... // Some other code, then
swap(&i, &j);

This has no effect on the original array. You should be passing p+n-1 and findLargest(a, n) directly, or store their results in pointers, not in ints:

swap(p+n-1, findLargest(a, n));

In addition, your swap is broken: rather than swapping the content of two pointers, it assumes that p points one element past the target location. This is a bad assumption to make in a general-purpose function, such as swap, and it also leads to undefined behavior in your program.

void swap(int *p, int *q) {
    int temp = *p;
    *p = *q;
    *q = temp;
}
Sign up to request clarification or add additional context in comments.

Comments

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.