0

Following is the code that I've written for helpers.c and it uses Selection sort and Binary Search

[code removed to comply to academic honesty]

/**
 * Sorts array of n values.
 */
void sort(int values[], int n)
{
    // TODO: implement an O(n^2) sorting algorithm i.e. Selection Sort
    int temp;
    for (int i = 0; i < n - 1; i++)
    {
        int min = values[i];

        for (int j = i + 1; j < n; i++)
        {
            if(values[j] < values[min])
            min = values[j];

            if (min != values[i])
            {
                temp = values[min];
                values[min] = values[i];
                values[i] = temp;
            }
        }

    }

}

I'm unable to figure out that why is it giving me segmentation fault error whenever I run it. What is causing this bug?

EDIT:

Code for Binary Search

bool search(int value, int values[], int n)
{
    // TODO: implement a searching algorithm i.e. Binary Search
    if (n == 1 && values[0] == value)
    {
        return true;
    }

    while (n > 1)
    {
        int middle = round(n / 2); 

        if (value == values[middle])
        {
            return true;
        }    
        else if (value < values[middle])
        {
            for(int p = middle - 1; p > 0; p--)
            {
                if (value == values[p])
                {
                    return true;
                }
            }
        }
        else if (value > values[middle])
        {
            for(int p = middle + 1; p <= n; p++)
            {
                if (value == values[p])
                {
                    return true;
                }

            }
        }
        else
            return false;

    }
    return false;
}
1

On your inner for loop you have:

for (int j = i + 1; j < n; i++)

when you should be incrementing j instead of i. This will solve your segfault but then you will have an infinite loop since you haven't implemented right your sort function. I would suggest you try again on your own and if you find it difficult to grasp how selection sort works, rewatch the videos/try a different sort method that seams easier to you/ask again for further help.

If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark.

Edit after comment

Per the specification:

Re-write search in such a way that it uses linear search, returning true if value is in values and false if value is not in values. Take care to return false right away if n isn’t even positive.

8
  • So I'm not getting the segfault now. But as you pointed, the application is now running into Inifinite loop. Any hint on this? please give hint only and not the sol – Anoneemus Aug 12 '15 at 12:09
  • gotcha! Now it's working perfectly! And thanks for pointing out the stupid mistake that I did. – Anoneemus Aug 12 '15 at 12:11
  • No mistake is stupid. It's just a mistake. Keep learning! – ChrisG Aug 12 '15 at 12:31
  • I've fixed almost everything. I'm now just facing one issue. How do I handle an event if the needle isn't found in the haystack? – Anoneemus Aug 12 '15 at 12:35
  • Check the edited answer. – ChrisG Aug 12 '15 at 12:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .