#include <stdio.h>

void swap(int *, int *);
void bubble_sort(int *, int);

#define SIZE 10

int main(void)
{
  int arr[SIZE] = {125, 52, 34, 80, 200, 12, 29, 102, 53, 89};

  bubble_sort(arr, SIZE);

  for (int i = 0; i < SIZE; i++) {
    if (i == SIZE-1)
      printf("%d\n", arr[i]);
    else
      printf("%d, ", arr[i]);
  }
}

void swap(int *x, int *y)
{
  int temp;

  temp = *x;
  *x = *y;
  *y = temp;

  return;
}

// Sorts an array of integers containing a specified numbre of elements
// Uses bubble sort
void bubble_sort(int list[], int numElements)
{
  int index;
  int isSorted;	// True if list is in sorted order, false otherwise

  do
  {
    isSorted = 1; // Start off assuming that list is sorted

    // Inner loop does one pass of bubble sort
    for (index = 0; index < numElements - 1; index++)
    {
      if (list[index] > list[index + 1])
      {
	// Adjacent elements are out of order; swap them
	swap(&list[index], &list[index+1]);
	isSorted = 0; // List was not sorted
      }
    }
  } while (!isSorted);

  return;
}
