Thread: recursive selection sort..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    recursive selection sort..

    i built this code exactly like in the theory
    the min function works

    where is the problem
    why its not sorting
    ??
    Code:
    #include <stdio.h>
    int min( int num[], int i, int j );
    void swap(int num[],int i,int j);
    void selection_sort(int arr[], int size);
    void selSort(int array[], int i,int length);
    
    int main()
    {
    	int array[3];
    	 array[0]=2;
    	 array[1]=6;
    	 array[2]=3;
         selection_sort(array,3);
         printf("%d,%d,%d\n",array[0],array[1],array[2]);
    
     
    
       return 0;
    }
    
    
    
     
    void selection_sort(int arr[], int size){
      selSort(arr,0,size);
    }
    
    void selSort(int array[], int i,int length)
    {
    
    	if (i<length)
                 { 				 
    			 swap(array,i,min(array,0,i));
    			 selSort(array,i+1,length); 
    		}
    
    }
     
    
     int min( int num[], int i, int j )
    {
      if ( i > 0 )
    {
      if ( num[i-1] < j )
    {
      return min( num, i-1, num[i-1] );
    } 
    else
    {
      return min( num, i-1, j);
    }
    } 
    else
    {
      return j;
    }
    }
    
    void swap(int num[],int i,int j)
    {
    int temp;
    temp=num[i];
    num[i]=num[j];
    num[j]=temp;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Instead of going through so many hoops; the recursive min() you have is enough to sort the array provided you hand out pieces of it inside a for loop.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant use a for loop

    only recurtion
    Last edited by transgalactic2; 01-13-2009 at 02:10 AM.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    when calling min inside min - the last parameter is an array value

    when calling min inside sel_sort - the last parameter is an array index

    this will not work together

    you should think about better names for parameters than i,j
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    the min function works
    i tried it
    so i dont need to look inside it

    the question is whether i implemented the algorithm of the main selection_sort(int arr[], int size)
    and selsort
    ??

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by transgalactic2 View Post
    the min function works
    i tried it
    so i dont need to look inside it

    the question is whether i implemented the algorithm of the main selection_sort(int arr[], int size)
    and selsort
    ??
    it works when used correcly

    you are using it incorrectly
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how am i supposed to use it in this algorithm?
    the minimum function find the minimum from the starting index to the end index

    thats what i did
    Last edited by transgalactic2; 01-13-2009 at 02:33 AM.

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    where is the problem in the implementation of the algorithm
    ??

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i did it like it was told here

    http://en.wikipedia.org/wiki/Selection_sort

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If I'm not mistaken you should find the minimum item in the unsorted part of the array, that is between num[i]...num[length], not num[0]...num[i].

    And the minimum function appears to be wrong for reasons others have noted.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    my min function is fine
    this is an example of its working fine

    Code:
    #include <stdio.h>
    int min( int num[], int i, int j );
    
    
    int main()
    {
    	int array[3];
    	
    	 array[0]=2;
    	 array[1]=6;
    	 array[2]=3;
        
    
    printf("%d\n",min(array, 3,3));
    
     
    
       return 0;
    }
    
    
    //j is the last member in the array
    //i is the number of cells
     
     int min( int num[], int i, int j )
    {
    if ( i > 0 )
    {
    if ( num[i-1] < j )
    {
    return min( num, i-1, num[i-1] );
    } else {
    return min( num, i-1, j);
    }
    } else {
    return j;
    }
    }
    i changed it to
    Code:
    swap(array,i,min(array,length,i));
    but it keeps the array as it is
    Last edited by transgalactic2; 01-13-2009 at 05:31 AM.

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    it does nothing with the array

    Code:
    #include <stdio.h>
    int min( int num[], int i, int j );
    void swap(int num[],int i,int j);
    void selection_sort(int arr[], int size);
    void selSort(int array[], int i,int length);
    
    int main()
    {
    	int array[3];
    	 array[0]=2;
    	 array[1]=6;
    	 array[2]=3;
         selection_sort(array,3);
         printf("%d,%d,%d\n",array[0],array[1],array[2]);
    
     
    
       return 0;
    }
    
    
    
     
    void selection_sort(int arr[], int size){
      selSort(arr,0,size);
    }
    
    void selSort(int array[], int i,int length)
    {
    
    	if (i<length)
                 { 				 
    			 swap(array,i,min(array,length,i));
    			 selSort(array,i+1,length); 
    		}
    
    }
     
    
     int min( int num[], int i, int j )
    {
      if ( i > 0 )
    {
      if ( num[i-1] < j )
    {
      return min( num, i-1, num[i-1] );
    } 
    else
    {
      return min( num, i-1, j);
    }
    } 
    else
    {
      return j;
    }
    }
    
    void swap(int num[],int i,int j)
    {
    int temp;
    temp=num[i];
    num[i]=num[j];
    num[j]=temp;
    }

  13. #13
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Try tracing the program with a debugger.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i did
    it just doesnt swap anything

    why?

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And what will happen if you call it like
    Code:
    int main()
    {
    	int array[3];
    	
    	 array[0]=2;
    	 array[1]=6;
    	 array[2]=3;
        
    
    printf("%d\n",min(array, 3,0));
    
     
    
       return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insertion and selection sort
    By Dashing Boy in forum C Programming
    Replies: 4
    Last Post: 08-29-2006, 04:42 PM
  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. selection sort records of chars
    By hew in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2002, 03:49 PM