Thread: recursive selection sort..

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The min function is not correct. Sometimes the third argument is an index, other times it is an array value. You compare array values with indices. Also it appears like it would find the min in the entire array whereas it needs to do that only for the unsorted part of the array.
    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).

  2. #17
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how to fix the min function?

  3. #18
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried to implement it again with the fixed minimum function
    it not sorting it
    ??

    Code:
    #include <stdio.h>
    void recSelSort(int array[],int length);
    void selSort(int array[], int i ,int length);
    void swap(int num[],int i,int j);
    int recFindMin(int array[], int index);
    
    int main()
    {
        int array[4] = {2, 1, 3,0};
    
    	recSelSort(array,4);
        printf("%d,%d,%d,%d\n", array[0],array[1],array[2],array[3]);
    
        return 0;
    }
    
     void recSelSort(int array[],int length)
    {
    	selSort(array,0,length);
    } 
    
    // sort array from i (till i is sorted and min)
     void selSort(int array[], int i ,int length)
    {    
    		if (i<length)
                 {
    				 swap(array,i,recFindMin(array,i));
    				 selSort(array,i+1,length); 
    		} 
    }	 
    
    
    int recFindMin (int array[], int index)
    {
        int local;
        if (index == 0)
        {
            return array[index];
        }
    
        local = recFindMin(array, index - 1);
        if (array[index] > local)
        {
            return local;
        }
        else
        {
            return array[index];
        }
    }
    
    void swap(int num[],int i,int j)
    {
    int temp;
    temp=num[i];
    num[i]=num[j];
    num[j]=temp;
    }

  4. #19
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what is the problem in the void selSort(int array[], int i ,int length) algorithm
    ??

  5. #20
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Do you really think it will swap when you are comparing array indices instead of the values in the function that calls swap(); and btw there's no ned for recSelSort().

  6. #21
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thats how the algorithm was presented to me

    what is the correct algorithm

  7. #22
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    my code is exactly like these words
    The algorithm works as follows:

    1. Find the minimum value in the list
    2. Swap it with the value in the first position
    3. Repeat the steps above for remainder of the list (starting at the second position)

    it start from index 0 etc..
    where is the problem

  8. #23
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i sorted it
    but there is a line i am corcerned abou
    i marked it by "// what about this line " comment
    ,i need to return the index of local not local itself??
    how to return its index
    ??
    Code:
    #include <stdio.h>
    void recSelSort(int array[],int length);
    void selSort(int array[], int i ,int length);
    void swap(int num[],int i,int j);
    int recFindMin(int array[], int index);
    
    int main()
    {
        int array[4] = {2, 1, 3,0};
    
    	recSelSort(array,4);
        printf("%d,%d,%d,%d\n", array[0],array[1],array[2],array[3]);
    
        return 0;
    }
    
     void recSelSort(int array[],int length)
    {
    	selSort(array,0,length);
    } 
    
    // sort array from i (till i is sorted and min)
     void selSort(int array[], int i ,int length)
    {    
    		if (i<length)
                 {
    				 swap(array,i,recFindMin(array,i));
    				 selSort(array,i+1,length); 
    		} 
    }	 
    
    
    int recFindMin (int array[], int index)
    {
        int local;
        if (index == 0)
        {
            return index;
        }
    
        local = recFindMin(array, index - 1);
        if (array[index] > local)
        {
            return local;  // what about this line 
        }
        else
        {
            return index;
        }
    }
    
    void swap(int num[],int i,int j)
    {
    int temp;
    temp=num[i];
    num[i]=num[j];
    num[j]=temp;
    }

  9. #24
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    my code is exactly like these words
    The algorithm works as follows:

    1. Find the minimum value in the list
    2. Swap it with the value in the first position
    3. Repeat the steps above for remainder of the list (starting at the second position)

    it start from index 0 etc..
    where is the problem
    If this is your algorithm you have to use recursion then you also need a loop to walk along the array starting from index 0, incrementing at each stage after min is found.

  10. #25
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i solved it look
    at post #23
    is it ok?

  11. #26
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i found an example for which my previous code is not working
    so i changed it to
    and its still doesnt sort
    Code:
    #include <stdio.h>
    void recSelSort(int array[],int length);
    void selSort(int array[], int i ,int length);
    void swap(int num[],int i,int j);
    int recFindMin(int array[], int index);
    
    int main()
    {
        int array[4] = {2, 1, 3,0};
    
    	recSelSort(array,3);
        printf("%d,%d,%d,%d\n", array[0],array[1],array[2],array[3]);
    
        return 0;
    }
    
     void recSelSort(int array[],int length)
    {
    	selSort(array,0,length);
    } 
    
    // sort array from i (till i is sorted and min)
     void selSort(int array[], int i ,int length)
    {    
    		if (i<length)
                 {
    				 swap(array,i,recFindMin(array,i));
    				 selSort(array,i+1,length); 
    		} 
    }	 
    
    
    int recFindMin (int array[], int index)
    {
        int local;
        if (index == 0)
        {
            return index;
        }
    
        local = recFindMin(array, index - 1);
        if (array[index] >array[local])
        {
            return local;  // what about this line 
        }
        else
        {
            return index;
        }
    }
    
    void swap(int num[],int i,int j)
    {
    int temp;
    temp=num[i];
    num[i]=num[j];
    num[j]=temp;
    }

  12. #27
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    is it from the assignment by zion siksik ?

  13. #28
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    i solved it look
    at post #23
    is it ok?
    so does it work or not? imo it's not complicated despite its recursive twist and the fact that recursive function calls have to be used as a substitute for the loop index.

  14. #29
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    it doesnt work

    i dont know where is the problem

  15. #30
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i changed it
    iits not sorting at all

    Code:
    #include <stdio.h>
    void recSelSort(int array[],int length);
    void selSort(int array[], int i ,int length);
    void swap(int num[],int i,int j);
    int recFindMin(int array[], int index);
    
    int main()
    {
        int array[4] = {2, 1, 3,0};
    
    	recSelSort(array,3);
        printf("%d,%d,%d,%d\n", array[0],array[1],array[2],array[3]);
    
        return 0;
    }
    
     void recSelSort(int array[],int length)
    {
    	selSort(array,length-1,length);
    } 
    
    // sort array from i (till i is sorted and min)
     void selSort(int array[], int i ,int length)
    {    
    		if (i<length)
                 {
    				 swap(array,i,recFindMin(array,i));
    				 selSort(array,i-1,length); 
    		} 
    }	 
    
    
    int recFindMin (int array[], int index)
    {
        int local;
        if (index == 0)
        {
            return index;
        }
    
        local = recFindMin(array, index - 1);
        if (array[index] >array[local])
        {
            return local;  // what about this line 
        }
        else
        {
            return index;
        }
    }
    
    void swap(int num[],int i,int j)
    {
    int temp;
    temp=num[i];
    num[i]=num[j];
    num[j]=temp;
    }

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