1

I'm a beginner to programming and I'm working through K.N.King's "C Programming: A Modern Approach". Right now I'm trying to do programming project 1 of chapter 9 but I keep getting a segfault and I'm not sure what's wrong with the code. Any correction is appreciated!

These are the instructions:

Write a program that asks the user to enter a series of integers (which it stores in an array), then sorts the integers by calling the function selection_sort. When given an array with n elements, selection_sort must do the following:
1. Search the array to find the largest element, then move it to the last position in the array.
2. Call itself recursively to sort the first n - 1 elements of the array.

And this is my code:

#include <stdio.h>

void selection_sort(int n, int a[n]);

int main(void)
{
    int n;

    printf("Number of integers to sort: ");
    scanf("%d", &n);

    int a[n];

    printf("Enter the integers: ");
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }

    selection_sort(n, a);

    printf("Sorted array: ");
    for (int i = 0; i < n; i++)
        printf("%d", a[i]);

    return 0;
}

void selection_sort(int n, int a[n])
{
    int swap, max = a[n - 1];

    for (int i = 0; i < n; i++) {
        if (a[i] > max) {
            max = a[i];
            swap = a[n - 1];
            a[n - 1] = max;
            a[i] = swap;
        }
    }
    if (n > 1)
        selection_sort(n - 1, a);
}

EDIT: changed while (n > 1) to if (n > 1) and now it works perfectly. But why wouldn't it work with while?

4
  • Where does it segfault? Commented Jul 23, 2016 at 18:47
  • 5
    Hello infinite recursion... you need to break the recursion at some point (using a base case that checks if it should stop). Commented Jul 23, 2016 at 18:47
  • When learning C, it's absolutely essential that you also learn to use a debugger. Single-stepping this code would make it clear where the problem lies. Commented Jul 23, 2016 at 18:48
  • Or just print parameters at start of selection_sort Commented Jul 23, 2016 at 18:49

2 Answers 2

3

You need a condition for not calling selection_sort. As it is now, your selection_sort always calls selection_sort, which results in an endless loop.

And, since you decrement n at each step, at one point it becomes negative, and you start accessing a[-1], which is wrong.

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

3 Comments

The condition must be n > 0 instead of n >= 0, since you calculate n - 1 later. And, since this is a sorting algorithm, n > 1 is even better because sorting an array with 1 element is unnecessary.
Thank you, now it doesn't segfault, but it hangs while waiting for the integers to be entered, it seems to be stuck in the loop for assigning each value to the corrisponding element in the array.
In that case, you need to check whether scanf succeeded. Only when it returns 1 (the number of fields correctly read), should you continue the program. I have heard rumors that writing scanf(" %d", &var) with a space in front of the %d helps.
2

You need to have some base condition for recursion to avoid endlessly calling the same function.You can do something like

void selection_sort(int n, int a[])
{
    if(n<=0)return;  //base condition
    int swap, max = a[n - 1];

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.