1. ## Please explain this code !!!! :(

I got this code for selection sorting, with the functions to sort set up for recursive calls.

I don't understand this code.

Could someone explain to me the logic??
basically loop by loop -- function by function

I did find how to do it without recursion, and I actually get that.

Also, do you think there has to be two separate functions, one to sort in ascending and descending orders; or can they be done in one function with a switch or if statement???

Also, to find the index of the smallest and largest element of original array, does this have to be split into two separate
functions, or again can this be accomplished with one function that finds both, yet recalls the values for sorting once in ascending order and once in descending order???

if it would be suffice, you can email me directly at
unique1_63301@yahoo.com

PHP Code:
``` #include <stdio.h> #include <stdlib.h> #include <time.h> #define Num_elem 10 #define Max_elem 50 void print_array(int x[], int n); void place_smallest(int[], int); void place_largest(int[], int); void selection_sort(int x[],int n) {     if (n > 1)  {         place_largest (x, n);         selection_sort (x, n - 1);    }    return; } void place_largest(int x[], int n) {     int temp;     int max_index;     int j;     max_index = n - 1;     for (j = n - 2; j >= 0; j--)         if (x[j] > x[max_index])         max_index = j;        if (max_index != (n - 1))    {            temp = x[n - 1];            x[n - 1] = x[max_index];            x[max_index] = temp;        }       return; } void selection_sort2(int x[],int n) {    if (n > 1)    {     place_smallest (x, n);     selection_sort2 (x, n - 1);    }    return; } void place_smallest(int x[], int n) {     int temp;     int min_index;     int j;     min_index = n - 1;     for (j = n - 2; j >= 0; j--)           if (x[j] < x[min_index])     min_index = j;     if (min_index != (n - 1))   {         temp = x[n - 1];         x[n - 1] = x[min_index];         x[min_index] = temp;     }     return; } main() {    int a[Num_elem];    int i = 0, j = 0;    srand((unsigned) time(NULL));    for (i = 0; i < Num_elem; i++)        a[i] = rand() % Max_elem + 1;    print_array(a, Num_elem);    selection_sort(a, Num_elem);    printf("Sorted array in ascending order:\n");    print_array(a, Num_elem);    printf("\n");    selection_sort2(a, Neum_elem);    printf("Sorted array in desending order:\n");    print_array(a, Num_elem);    return 0; } void print_array(int x[], int n) {    int i;    for (i = 0; i < n; i++)      printf("   %5d", x[i]);      printf("\n"); }  ```

2. is this better, Salem???

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define Num_elem 10
#define Max_elem 50

void print_array(int x[], int n);
void place_smallest(int[], int);
void place_largest(int[], int);

void selection_sort(int x[],int n)
{
if (n > 1)  {
place_largest (x, n);
selection_sort (x, n - 1);
}
return;
}

void place_largest(int x[], int n)
{
int temp;
int max_index;
int j;
max_index = n - 1;

for (j = n - 2; j >= 0; j--)
if (x[j] > x[max_index])
max_index = j;

if (max_index != (n - 1))    {
temp = x[n - 1];
x[n - 1] = x[max_index];
x[max_index] = temp;
}
return;
}

void selection_sort2(int x[],int n)
{
if (n > 1)    {
place_smallest (x, n);
selection_sort2 (x, n - 1);
}
return;
}

void place_smallest(int x[], int n)
{
int temp;
int min_index;
int j;
min_index = n - 1;
for (j = n - 2; j >= 0; j--)
if (x[j] < x[min_index])
min_index = j;

if (min_index != (n - 1))   {
temp = x[n - 1];
x[n - 1] = x[min_index];
x[min_index] = temp;
}
return;
}

main()
{
int a[Num_elem];
int i = 0, j = 0;
srand((unsigned) time(NULL));
for (i = 0; i < Num_elem; i++)
a[i] = rand() % Max_elem + 1;
print_array(a, Num_elem);
selection_sort(a, Num_elem);

printf("Sorted array in ascending order:\n");
print_array(a, Num_elem);
printf("\n");
selection_sort2(a, Neum_elem);
printf("Sorted array in desending order:\n");
print_array(a, Num_elem);
return 0;
}

void print_array(int x[], int n)
{
int i;

for (i = 0; i < n; i++)
printf("   %5d", x[i]);
printf("\n");
}```