I am writing recursive function to sort a small array. It is supposed to be the selection sort, and I know there is something wrong with it. In DevC++ works OK, sorts, output good etc. In Vis C++ makes a mess of my array, adds some new numbers. In ggc on my unix server, there is simply core dumped. I think I must have gone out of array boundaries??? Could someone take a look at my code and see what I might be doing wrong?
Code:
#define LEN 10
int find_large(int [], int);
void swap(int *, int *);
void selec_sort(int [], int);
void seed_array(int [], int);
int main()
{
int array[LEN];
srand(time(NULL));
seed_array(array, LEN);
selec_sort(array, LEN);
return 0;
}
and the functions def:
Code:
void seed_array(int a[], int n)
{
int i;
for(i = 0; i < n; i++) {
a[i] = rand() % 50 + 1;
printf("Array[%d] is %d\n", i, a[i]);
}
}
int find_large(int a[], int n)
{
int large = a[0];
int maxi, i;
for(i = 0; i < n; i++)
{
if(a[i] > large)
{
large = a[i];
maxi = i;
}
}
return maxi;
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void selec_sort(int a[], int n)
{
int large_ind;
if(n > 1)
{
large_ind = find_large(a, n);
swap(&a[large_ind], &a[n-1]);
selec_sort_rec(a, n-1);
}else
return;
return;
}