简体   繁体   中英

Segmentation Fault in my Quick Sort Program 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]);
}

see image here...

In the image, the high value ( 6422295 ) is quite large as for the size of array. The large size array would lead to the segmentation fault.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM