Now I have worked on this for quite a long time.
I have two problems.
First and foremost, I need the sorting functions to be coded with recursive calls (I don't know how to do that from what I got here with for loops)
What is going-up recursion and going-down recursion?
I am reading that I need going-down recursion to sort in ascending order ; and going-up recursion for descending order.
What does that mean.....exactly???
Secondly, I cannot get the descending order function to work quite right. I think it is taking the first element of the original array and assigning it to array[0], then it sorts the rest in descending order. What am I doing wrong???
PHP Code:
#include <stdio.h>
#define NUMELEM 10
#define MAXELEM 200
void select_sort_ascend(int array[], int s);
void select_sort_descend(int array[], int s);
void print_array (int array[], int s);
main()
{
int i;
int array[NUMELEM];
/**************************************/
/* fill array with random numbers */
/**************************************/
for (i = 0; i < NUMELEM; i++) {
array[i] = rand()%MAXELEM+1;
}
/**************************************/
/* print random array */
/**************************************/
printf("The random array : \n\n");
print_array(array, NUMELEM);
/**************************************/
/* sort array into ascending order */
/* using selection sorting */
/**************************************/
select_sort_ascend(array,NUMELEM);
/**************************************/
/* print array in ascending order */
/**************************************/
printf("The random array in ascending order: \n\n");
print_array(array,NUMELEM);
/*************************/
/* sort array in */
/* descending order */
/*************************/
select_sort_descend(array, NUMELEM);
/**************************************/
/* print array in descending order */
/**************************************/
printf("The random array in descending order: \n\n");
/**************************************/
/* print array in descending order */
/**************************************/
print_array(array,NUMELEM);
return 0;
}
/***********************/
/* END OF MAIN */
/***********************/
void select_sort_ascend(int array[],int s)
{
int i, j, smallest, temp;
for (i=0;i<NUMELEM;i++) {
smallest=i;
for (j=i; j<NUMELEM;j++) {
if (array[smallest] > array[j]) {
smallest = j;
}
}
temp = array[i];
array[i] = array[smallest];
array[smallest] = temp;
}
}
void select_sort_descend(int array[],int s)
{
int i, j, largest, temp;
for (i=NUMELEM;i>0;i--) {
largest=i;
for (j=i;j>0;j--) {
if (array[j] < array[largest]) {
largest = j;
}
}
temp = array[i];
array[i] = array[largest];
array[largest] = temp;
}
}
void print_array(int array[],int s)
{
int i;
for (i=0; i<NUMELEM; i++) {
printf("%3d ",array[i]);
if ((i+1)%5 == 0)
printf("\n");
}
}