/*
 *  This program sorts a file of up to LIMIT integers with bubblesort
 *
 *  Author: Jost Berthold, Philipps-Universität Marburg, FB 12
 *
 *  Inspired by 
 *     http://www.cs.loyola.edu/~delcher/CS201/
 *********************************************************************
 *
 * Uebersetzung und Start des Programms mit gcc unter Linux:
 * berthold@maseru:>gcc -o bubsort bubblesort_seq.c
 * berthold@maseru:>./bubsort 80Numbers.txt
 * 80 numbers read.
 * [2560, -937, ...]
 * After Sort:
 * [-4961, -4950, ...
 * berthold@maseru:>                        
 */

#include  <stdio.h>

// #include  <limits.h> // sizes for C-data types
// #include  <stdlib.h> // for random number generator

#define LIMIT 1000

void  Print_Array  (int A [], int N);
void  Sort  (int A [], int N);
void  Swap  (int* X, int* Y);
void  readNumbers(int array[], int* limit, char* filename);


int numbers[LIMIT]; // the array to sort, limited to 1000 elements
int size = LIMIT; // initialized with LIMIT elements,
                  // value after reading depends on file size.


main  (int argc, char* argv[])
  {
  if (argc < 2) { // no file => terminate.
    fprintf(stderr, "Usage: %s <file with numbers>\n",argv[0]);
    fprintf(stderr, "\t sorts the (up to %d) numbers in the file\n",LIMIT);
    exit(1);
    }

  // read the numbers from a file
  readNumbers(numbers, &size, argv[1]); 
  // print the array to sort
  Print_Array (numbers, size);
  // sort the array
  Sort (numbers, size);
  // print the sorted array 
  printf ("\nAfter Sort:\n");
  Print_Array (numbers, size);
  
  return  0;
  }

void readNumbers(int array[], int* size, char* filename)
  {
    FILE* opened;  // handle to given file
    char buf[30];
    int  number; // number for read elem
    int  counter = 0; // counter for elems in file

  opened = fopen(filename, "r"); // open file for reading
  
  while (!feof(opened) && counter < *size) 
    { // read until EOF or too many elements
      fgets(buf, 30, opened); // read one line, max 30 characters
      number = atoi(buf); // convert to integer
      array[counter++] = number; // set value in array
      if ( counter >= *size) // too many numbers for the array... cut rest. 
	  fprintf(stderr, "...too many numbers, cutting rest.\n",counter++);
    }
  fclose(opened);

  *size = --counter; // set size to correct value
  fprintf(stdout, "%d numbers read.\n",*size);
  }

void  Print_Array  (int A [], int N)
//  Print contents of  A [0 .. (N-1)] .
  {
   int  i;
   fprintf(stdout,"[");
   for  (i = 0;  i < N;  i ++)
     fprintf (stdout,"%d, ", A [i]);
   fprintf(stdout,"]\n");
  }

void  Sort  (int A [], int N)
//  Sort array  A [0 .. (N-1)]  with bubblesort
  {
   int  i, j;

   for  (i = 0;  i < N - 1;  i ++)
     for  (j = 0;  j < N - i - 1;  j ++)
       if  (A [j] > A [j + 1])           // Out of order
           Swap (& A [j], & A [j + 1]);
  }

void  Swap  (int* X, int* Y)
//  Exchange the contents of  * X  and  * Y .
  {
   int Save = *X;
   * X = * Y;
   * Y = Save;
  }

