0

this is my code.

bool search(int value, int values[], int n)
{
    int max = n -1;
    int min = 0;
    int mid;

    while(n > 0)
    {
        mid = ((min = max)/2);

        if(values[mid] == value)
        {
            return true;
        }
        else if(values[mid] > value)
        {
            max = mid - 1;
            return search(value,values,n);
        }
        else if(values[mid] < value)
        {
            min = min + 1;
            return search(value,values,n);
        }
        return false;
    }
    return 0;
}

/**
 * Sorts array of n values.
 */
 void sort(int values[], int n)
 {
    int min;
    int swap;

    for(int i = 0; i > (n - 1); i++)
    {
        min = i;
        for(int j = (i + 1); j > n; j++)
        {
            if(values[j] < values[min])
            {
                min = j;
            }
            if(min != i)
            {
                swap = values[min];
                values[min] = values[i];
                values[i] = swap;
            }
        }
    }
    return;
}

when i run this program it shows segmentation error. i think that my code is correct but i can't figure out why segmentation error is occuring.

0

Sort is broken. Because of this, search fails. Look at the for loop setups in your sort. How many times will they execute? You should consider printing out the sorted list to diagnose the sort routine and to prove the list is sorted.

This doesn't guarantee that there are no problems with search, but you need to fix one issue at a time. ;-) Also, I would be very careful about returning a number instead of true/false for a bool return type.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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