1. ## 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. 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. i cant use a for loop

only recurtion

4. 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

5. 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. Originally Posted by transgalactic2
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

7. 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

8. where is the problem in the implementation of the algorithm
??

9. i did it like it was told here

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

10. 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.

11. 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

12. 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. Try tracing the program with a debugger.

14. i did
it just doesnt swap anything

why?

15. 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;
}```