# recursive sort

• 03-05-2005
bethan
recursive sort
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; }```
• 03-06-2005
Salem
Code:

```gcc -W -Wall -ansi -pedantic -O2 hello.c hello.c: In function `find_large': hello.c:40: warning: `maxi' might be used uninitialized in this function```
On closer examination, it seems that if the largest value in the array is indeed at a[0], then you will never set maxi, and you'll return some random value.
Obviously using some random value as an array index leads to some "bad news" for you.
• 03-06-2005
bethan
Thanks, Salem. It works now well. And thanks for showing me how to check for errors in unix, none of my instructors have ever mentioned this option.