-2

I am new to programming and currently I am trying to write an algorithm for Selection Sort in recursion. I tried to pass the array[10] into the selection sort function I define before the main function and the selection sort function calls another function. After compiling this, it doesnt sort the array for some reason..Instead it adds an integer into the beginning of the array .I cant figure out what is wrong...

void helper_func(int arr[],int x,int y)
{    

    int tmp=0;
    if(y<= (sizeof(arr))/(sizeof(int)))
    {    
        if(arr[x]>arr[y])
        {  
            tmp=arr[x];
            arr[x]=arr[y];
            arr[y]=tmp;     
        }
            y=y+1;
            helper_func(arr,x,y);

        }
}

void SelectionSort(int arr[], int len) 
{   

    if (len>1)
    {   
        int x=sizeof(arr)/sizeof(int)-len;
        int y=x;        
        helper_func(arr,x,y);
        len=len-1;
        SelectionSort(arr,len);
    }

}



int main()
{       

     int array_a[10]={10,9,8,7,6,5,4,3,2,1};
     SelectionSort(array_a, 10);
     int x=0;
     for(x=0;x<=sizeof(array_a)/sizeof(int)-1;x++)//checking what is in the arry
     {     
         printf("%d...",array_a[x]);
     }

     return 0;
}
styvane
  • 59,869
  • 19
  • 150
  • 156
GalaxyVintage
  • 656
  • 1
  • 11
  • 20

3 Answers3

1

"The sizeof way is the right way if you are dealing with arrays not received as parameters. An array sent as a parameter to a function is treated as a pointer, so sizeof will return the pointer's size, instead of the array's.

Thus, inside functions this method does not work. Instead, always pass an additional parameter size_t size indicating the number of elements in the array."

You may refer this question : How do I determine the size of my array in C?

In your case sizeof(arr) return 4, which is actually size of pointer.

Community
  • 1
  • 1
Niyas C
  • 11
  • 1
0

Alright, the problem is that your method of determining the length of the array (by calculating sizeof(arr)/sizeof(int)) is not working.

This is because when an array is passed to a function in C, what is actually passed is a pointer to the first element of the array. sizeof(arr) in the functions SelectionSort and helper_func will always return 8, because 8 is the size of a pointer (well, at least on my machine).

sizeof(array_a) in your main function works, but that is simply because you declared the array there, and the complier is able to be smart and tell you the size of the whole array.

In C for an array like this, there is no way that I'm aware of to determine the length of the array that was passed to a function (i.e., a pointer to the first element of the array) without any other information. Usually what is done is that the length of the array is simply passed to the function that needs it, when the array is passed.

For example you could change void SelectionSort(int arr[], int len) to take void SelectionSort(int arr[], int len, int overall_len) and pass the length of the array along with it. Although in this case, the variable names are confusing, and it would probably be better to change len to counter or something. You'll have to change this for helper_func as well. I did this, and the code worked better (although you'll have to tweak your algorithm as well, as it didn't quite sort correctly).

Nathan
  • 73,987
  • 14
  • 40
  • 69
  • Oh, and this is a stylistic suggestion that's unrelated to the problem, but you might want to standardize it so that SelectionSort and helper_func use the same naming convention, as opposed to one using camelCase and the other using underscores. You can look up some stuff on naming conventions in C, there's lots of articles out there. – Nathan Feb 11 '15 at 05:22
  • hmm I see. I am wondering if it is possible to write an algorithm with recursive functions only.(no for loop or while loop) – GalaxyVintage Feb 12 '15 at 05:34
  • Theoretically, everything that can be done with a loop can be done with recursion. – Nathan Feb 12 '15 at 08:42
-1

Try this:

for(int x=0; x<n; x++)
{
    int index_of_min = x;
    for(int y=x; y<n; y++)
    {
        if(array[index_of_min]>array[y])
        {
            index_of_min = y;
        }
    }

    int temp = array[x];
    array[x] = array[index_of_min];
    array[index_of_min] = temp;
}
Nic Wortel
  • 11,155
  • 6
  • 60
  • 79