/* This is a demo program that takes input in an array 
and sorts those values using selection sort*/ 

#include<stdio.h>
#define MAX 100
/* Q: What is #define? */

int main(void)
{
  int i, j, t, indexOfMax, array[MAX], n;
  
  /* Take the input size of the array*/ 
  printf("\n What is the size of the array (it should be less than %d)::> ",MAX);
  scanf("%d", &n);

  /* Take the input to the array */
  for(i=0;i<n;i++)
   {
     printf("\n Enter the %d-th element::> ",i+1);
     scanf("%d",&array[i]);
   } 
  
   /* Logic for selection sort. The maximum element is found and stored in
      array[n-1] and then 2nd maximum is found and stored in array[n-2] and so
      on. The index 'indexOfMax' keeps track of that location of the current
      maximum element which is initialised to 'i'. The index 'j' scans the 
      array upto that location for the maximum. 
   */
    for (i=n-1; i>0; i--) {
	indexOfMax = i;
      	for (j=0; j<i; j++) {   
         if (array[j] > array[indexOfMax])/* Checks if updation in index of Max needed*/   
	     indexOfMax = j;    
	}
	/* The next 3 lines does the swap */
	t = array[i];          
        array[i] = array[indexOfMax];     
        array[indexOfMax] = t; 
      
   }
  
  printf("\n The sorted output is ::> "); 
  for(i=0;i<n;i++) {
     printf(" %d ",array[i]);
   } 
  printf ("\n ");
  return 0;
}

