'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]);
}
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | imjlfish |
