/* Example: bubble sort values in array */

/* The bubble sort works by comparing values in adjacent
array positions. If the value in the "lower" position is 
greater than that in the "higher" position, the two are 
swapped. Thus the largest value "bubbles up" to the "top" 
of the array. When there are no more values to be swapped, 
sorting is complete. */

#include <stdio.h>


void bubblesort_float_generic(float *firstIndexOfArray, int arrayLength, int (*compare_fn)(float*,float*) ){


/*could create a float array here and then do the sort on it */

 int i, sorted, swaps = 0, iterations=0;
 float* currentIndexOfArray;
 float* oneLessCurrentIndex;
 float temp;

 

/* Bubble sort: */

 do {
  currentIndexOfArray=firstIndexOfArray+1;
  for (i = 1, sorted = 1; i < arrayLength; i++)
  {
    oneLessCurrentIndex=currentIndexOfArray-1;

    if (compare_fn(currentIndexOfArray,oneLessCurrentIndex))
    {
      sorted = 0;
      temp = *oneLessCurrentIndex;
      *oneLessCurrentIndex = *currentIndexOfArray;
      *currentIndexOfArray = temp;
    }
    currentIndexOfArray++;

  }
} while (!sorted);


/*print out sorted values */

printf("The sorted values are:\n");

currentIndexOfArray=firstIndexOfArray;

for (i = 0; i < arrayLength; i++){
  printf("%.3G ",*currentIndexOfArray);
  currentIndexOfArray++;
}

printf("\n");

}


sortAscendingOperator(float* a, float* b){

  if(*a<*b){
    return 1;
  }else{
    return 0;
  }

}

sortDescendingOperator(float* a, float* b){

  if(*a>*b){
    return 1;
  }else{
    return 0;
  }

}






main()
{
  int i;
  int arraySize=0;

  float* firstIndexOfArray;
  float* currentIndexOfArray;
  float tempValueForInput;

  /* Input values: */
  currentIndexOfArray=firstIndexOfArray;


  do{
    scanf("%f",&tempValueForInput);
    if(tempValueForInput!=0){
      *currentIndexOfArray=tempValueForInput; //store this value
      currentIndexOfArray++;//point to the next location
      arraySize++; //keep track of array size
    }
  }while(tempValueForInput!=0);


   /*print out the original values */

  printf("The original values are:\n");

  currentIndexOfArray=firstIndexOfArray;

  for (i = 0; i < arraySize; i++){
    printf("%.3G ",*currentIndexOfArray);
    currentIndexOfArray++;
  }

  printf("\n");

/* sort ascending */
  bubblesort_float_generic(firstIndexOfArray,arraySize,&sortAscendingOperator);


/* sort decending */
  bubblesort_float_generic(firstIndexOfArray,arraySize,&sortDescendingOperator);





}


