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");
}