Thread: recursive sort

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    Kaliningrad
    Posts
    5

    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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Location
    Kaliningrad
    Posts
    5
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. recursive quick sort - stack overflow
    By Micko in forum C Programming
    Replies: 9
    Last Post: 01-01-2005, 05:51 PM
  3. Recursive Array Sort
    By Nakeerb in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2002, 08:27 PM
  4. recursive selection sort function
    By Cornelius in forum C Programming
    Replies: 3
    Last Post: 11-08-2002, 04:12 PM
  5. recursive insertion sort.
    By Alien_Freak in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2002, 01:31 AM