/* 13(a) W.A.P. to accept n integers and perform Selection Sort */

#include<stdio.h>
void main()
{
    void exchg(int n,int num[]); /* Function Prototype */
    int i,n,*num; /* Variable Declarations */
    clrscr();
    printf("How many elements or numbers in your list : ");
    scanf("%d",&n);
    if (n>0)
    {
        for (i=0; i<n; i++)
        {
            printf("Number %d = ",i+1);
            scanf("%d",&num[i]);
        }
        exchg(n,num); /* Call the function 'exchg' to sort the given list */
        printf("\n\nAbove elements are sorted as follows :\n");
        for (i=0; i<n; i++)
        {
            printf("\n%d",num[i]);
        }
    }
    else printf("\n!!! Invalid Input Entry. Please Try Again");
    getch();
}
void exchg(int n,int num[]) /* Function Definition 'exchg' */
{
    int fmax(int m,int num[]);
    int tmp,j,big;
    /* Performs Selection Sort Operation */
    for (j=(n-1); j>=1; j--)
    {
        big=fmax(j,num); /* Call function 'fmax' */
        tmp=num[big];
        num[big]=num[j];
        num[j]=tmp;
    }
}
int fmax(int m,int num[]) /* Function definition 'fmax' */
{
    int k,maxret=0;
    /* returns the position of the bigger value in the list */
    for (k=1; k<=m; k++)
    {
        if (num[k]>num[maxret]) maxret=k;
    }
    return(maxret);
}