/************************************************************
 * This procedure uses the bubble sort algorithm to sort an * 
 * array of integers in ascending order. The procedure      *
 * receives the array and its size as parameters.           *
 ************************************************************/
 
 #define  UNSORTED  0
 #define  SORTED    1

 void bubble_sort (int x[], int size)
 {
        int  state;     /* records SORTED/UNSORTED status   */
        int  end_index; /* number of comparisons to be done */
        int  i, temp;
 
 /* Assume that the whole array is initially unsorted */
        state = UNSORTED;  
        end_index = size;

        while (state == UNSORTED)
        {
            state = SORTED;     /* Now prove that the array   
                                   is not sorted            */
            end_index--;

            for (i=0; i<end_index; i++)   /* pass loop */
            {
                /* Look at adjacent pairs of elements and swap
                   if the second element is less than the first */
                if (x[i] > x[i+1])
                {
                    temp = x[i];
                    x[i] = x[i+1];
                    x[i+1] = temp;
                    state = UNSORTED;
                }
            }
        }
 }

