Sort the given array in the descending order - selection sort.

#include <stdio.h>

void selection_sort(float *, const int);

int main(void)
{
float arr[5];
int i;

printf( "\nEnter 5 floats : " );

for( i = 0 ; i < 5 ; i++ )
scanf( "%f", &arr[i] );

selection_sort(arr, 5);

/* Displaying sorted array */
printf( "\nSorted Array : " );
for( i = 0 ; i < 5 ; i++ )
printf( "\n\t%f", arr[i] );

return 0;
}

void selection_sort(float *p, const int sz)
{
float max;
int i, j, maxpos;

/* Selection sort */
for( i = 0 ; i < (sz - 1) ; i++ )
{
max = p[i]; maxpos = i;
for( j = i + 1 ; j < sz ; j++ )
if( p[j] > max )
{
max = p[j];
maxpos = j;
}
/* Interchange the highest element with the current top element */
p[maxpos] = p[i];
p[i] = max;
}
}

No comments:

Post a Comment

kiss on google ads if you are anonymous because your ip is trackable.thank you.

......from.admin