Hello everyone,
I am learning C by my self and I would like to have some help with an exercise.
Task:
To create a recursive function to sort elements in an array of integers.
The function must start the sorting from the first element, and the recursion calls must go on until the last element in the array is sorted. In each step of recursion, the function must work only with the subset of array elements that have not been sorted yet.
Problem:
I am getting a 'Segmentation fault: 11' in the recursive call to the function (please, see the code below).
Environment:
Mac, OS X = Mavericks
Thanks a lot in advance,
Manuel
Code:
////////////////////////////////////////////////////////////////////////////////
////////// Recursively sorting an array of ints.
////////// Arguments: array, array size, index from where to start sorting.
void sort_incr_array_int_recursive(int a[], int size, int i){
int tmp, idx_small, small = a[i];
// Locating the smaller element in the array section.
for(int p = i; p < size; p++)
if(a[p] < small)
idx_small = p;
// Assigning the smaller element to the position
// currently evaluated.
tmp = a[i];
a[i] = a[idx_small];
a[idx_small] = tmp;
if(i < size) // The segmentation fault is caused by the instruction below
sort_incr_array_int_recursive(a, size, i + 1);
}
////////////////////////////////////////////////////////////////////////////////