
/* This is a demo program that takes input in an array 
and sorts those values using bubble sort*/ 

#include<stdio.h>
#define MAX 100
/* Q: What is #define? */

int main(void)
{
  int i, j, t, 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 bubble sort. The maximum element bubbles up 
      the part of the array under consideration to reach its 
      exact location. The index 'i' keeps track of that location  
      where the current maximum would go. The index 'j' scans the 
      array upto that location for the maximum. 
   */
    for (i=n-2; i>=0; i--) {    
      for (j=0; j<=i; j++) {   
         if (array[j] > array[j+1]) { /* Checks if swap is needed */       
            /* The next 3 lines does the swap */
            t = array[j];          
            array[j] = array[j+1];     
            array[j+1] = t;        
         }
      }
   }
  
  printf("\n The sorted output is ::> "); 
  for(i=0;i<n;i++) {
     printf(" %d ",array[i]);
   } 
  printf ("\n ");
  return 0;
}
