DEV Community

Owais Athar
Owais Athar

Posted on

Getting Segmentation Fault by Debugger in C.

I'm Getting Segmentation fault by debugger in partition() function at commented variable...following code is a complete implementation of Quick Sort algorithm.

// Sorting Algorithms

void quick_sort(int arr[],int length);
int partition(int a[],int low, int high);
void quick_sort_recursion(int arr[], int low, int high);
void swap(int *x, int *y);

void quick_sort(int arr[],int length){
    quick_sort_recursion(arr,0,length-1);

}

void quick_sort_recursion(int arr[],int low, int high){

    if(low<high){
        int pivot_value = partition(arr,low,high);
        quick_sort_recursion(arr,low,pivot_value - 1);
        quick_sort_recursion(arr,pivot_value+1,high);
    }
}

int partition(int arr[], int low, int high){

    int pivot_value = arr[high]; //getting 'segmentation fault' here
    int i = low;

    for (int j = low; j < high; j++)
    {
        if(arr[j] <= pivot_value){
            swap(&arr[i],&arr[j]);
            i++;
        }
    }
    swap(&arr[i],&arr[high]);
}

Enter fullscreen mode Exit fullscreen mode

see image here...

Oldest comments (0)