[英]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.我在
partition()
function 中的调试器在注释变量处出现分段错误...以下代码是快速排序算法的完整实现。
// 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]);
}
In the image, the high
value ( 6422295
) is quite large as for the size of array.在图像中,
high
值 ( 6422295
) 对于数组的大小来说非常大。 The large size array would lead to the segmentation fault.大尺寸数组会导致分段错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.