#include <stdlib.h>
#include <string.h>
-#include "main.h"
+#include "sort.h"
-
-void printlist(FILE *stream, int list_size, double *list, int num_shown)
+void printlist(FILE * stream, int list_size, double *list, int num_shown)
{
- int i;
- for(i=0; i < num_shown; i++)
- fprintf(stream, "%f\t", list[i]);
- fprintf(stream, "\n");
- for(i=num_shown; i > 0 ; i--)
- fprintf(stream, "%f\t", list[list_size-i]);
- fprintf(stream, "\n");
+ int i;
+ for (i = 0; i < num_shown; i++)
+ fprintf(stream, "%f\t", list[i]);
+ fprintf(stream, "\n");
+ for (i = num_shown; i > 0; i--)
+ fprintf(stream, "%f\t", list[list_size - i]);
+ fprintf(stream, "\n");
}
double checklist(int list_size, double *list)
{
- int i;
- double sum;
+ int i;
+ double sum;
- sum = 0.0;
- for(i = 0; i < list_size; i++ )
- sum = sum + list[i];
- return sum;
+ sum = 0.0;
+ for (i = 0; i < list_size; i++)
+ sum = sum + list[i];
+ return sum;
}
-int read_data(const char *file_name, int *pList_size, double **pList) {
- FILE *fp;
- int i;
- double x;
-
- // open the file
- if ((fp = fopen(file_name, "r")) == NULL)
- {
- fprintf(stderr, "error in opening data file %s\n", file_name);
- return EXIT_FAILURE;
- }
-
- // read the size of the data file
- fscanf(fp, "# %d", pList_size);
-
- // allocate memory for the data
- *pList = (double *)malloc(sizeof(double)* *pList_size);
- if (*pList == NULL)
- {
- fprintf(stderr, "could not allocate %d bytes\n",
- sizeof(double)* *pList_size);
- return EXIT_FAILURE;
- }
-
- // read in the data
- fprintf(stderr, "reading %d points\n", *pList_size);
- for (i=0; i < *pList_size ; i++)
- {
- fscanf(fp, "%lf", &x);
- (*pList)[i] = x;
- }
-
- // close the file
- fclose(fp);
- return EXIT_SUCCESS;
+int read_data(const char *file_name, int *pList_size, double **pList)
+{
+ FILE *fp;
+ int i;
+ double x;
+
+ // open the file
+ if ((fp = fopen(file_name, "r")) == NULL) {
+ fprintf(stderr, "error in opening data file %s\n", file_name);
+ return EXIT_FAILURE;
+ }
+ // read the size of the data file
+ fscanf(fp, "# %d", pList_size);
+
+ // allocate memory for the data
+ *pList = (double *)malloc(sizeof(double) * *pList_size);
+ if (*pList == NULL) {
+ fprintf(stderr, "could not allocate %d bytes\n",
+ sizeof(double) * *pList_size);
+ return EXIT_FAILURE;
+ }
+ // read in the data
+ fprintf(stderr, "reading %d points\n", *pList_size);
+ for (i = 0; i < *pList_size; i++) {
+ fscanf(fp, "%lf", &x);
+ (*pList)[i] = x;
+ }
+
+ // close the file
+ fclose(fp);
+ return EXIT_SUCCESS;
}
-
-int main ( int argc, char *argv[] )
+int main(int argc, char *argv[])
{
- int list_size, i;
- double *list;
- char *file_name = "data";
-
- // parse arguments
- if (argc > 1)
- file_name = argv[1];
-
- // setup
- if (read_data(file_name, &list_size, &list) != EXIT_SUCCESS)
- return EXIT_FAILURE;
-
- /* print initial list */
- fprintf(stderr, "The list before sorting is:\n");
- printlist(stderr, list_size, list, 3);
- fprintf(stderr, "Check: sum of %d elements = %f\n",
- list_size, checklist(list_size, list));
-
- /* sort the list */
- sort(list_size, list);
-
- /* print final list */
- fprintf(stderr, "The list after sorting is:\n");
- printlist(stderr, list_size, list, 3);
- fprintf(stderr, "Check: sum of %d elements = %f\n",
- list_size, checklist(list_size, list));
- for (i=0; i < list_size; i++)
- printf("%f\n", list[i]);
-
- return EXIT_SUCCESS;
+ int list_size, i;
+ double *list;
+ char *file_name = "data";
+
+ // parse arguments
+ if (argc > 1)
+ file_name = argv[1];
+
+ // setup
+ if (read_data(file_name, &list_size, &list) != EXIT_SUCCESS)
+ return EXIT_FAILURE;
+
+ /* print initial list */
+ fprintf(stderr, "The list before sorting is:\n");
+ printlist(stderr, list_size, list, 3);
+ fprintf(stderr, "Check: sum of %d elements = %f\n",
+ list_size, checklist(list_size, list));
+
+ /* sort the list */
+ sort(list_size, list);
+
+ /* print final list */
+ fprintf(stderr, "The list after sorting is:\n");
+ printlist(stderr, list_size, list, 3);
+ fprintf(stderr, "Check: sum of %d elements = %g\n",
+ list_size, checklist(list_size, list));
+ for (i = 0; i < list_size; i++)
+ printf("%g\n", list[i]);
+
+ return EXIT_SUCCESS;
}
*
* ref: http://cprogramminglanguage.net/quicksort-algorithm-c-source-code.aspx
* Numerical Recipes
- */
+ */
void swap(double *x, double *y)
{
- double temp;
- temp = *x;
- *x = *y;
- *y = temp;
+ double temp;
+ temp = *x;
+ *x = *y;
+ *y = temp;
}
-int choose_pivot(int i,int j )
+int choose_pivot(int i, int j)
{
- return((i+j) /2);
+ return ((i + j) / 2);
}
-void quicksort( double list[], int m, int n )
+/* sort the list from list[m] to list[n] */
+void quicksort(double list[], int m, int n)
{
- int i,j,k;
- double key;
-
- if( m < n)
- {
- k = choose_pivot(m,n);
- swap(&list[m],&list[k]);
- key = list[m];
- i = m+1;
-
- j = n;
- while(i <= j)
- {
- while((i <= n) && (list[i] <= key))
- i++;
- while((j >= m) && (list[j] > key))
- j--;
- if( i < j)
- swap(&list[i],&list[j]);
+ int i, j, k;
+ double key;
+
+ if (m < n) {
+ k = choose_pivot(m, n);
+ swap(&list[m], &list[k]);
+ key = list[m];
+
+ i = m + 1;
+ j = n;
+ while (1) {
+ while ((i <= n) && (list[i] <= key))
+ i++;
+ while ((j >= m) && (list[j] > key))
+ j--;
+ if (i >= j)
+ break;
+ swap(&list[i], &list[j]);
+ }
+
+ // put the pivot back in
+ swap(&list[m], &list[j]);
+
+ // recursively sort the lesser lists
+ quicksort(list, m, j - 1);
+ quicksort(list, j + 1, n);
}
- // swap two elements
- swap(&list[m],&list[j]);
-
- // recursively sort the lesser list
- quicksort(list,m,j-1);
- quicksort(list,j+1,n);
- }
}
void sort(int list_size, double *list)
{
- quicksort(list, 0, list_size-1);
+ quicksort(list, 0, list_size - 1);
}