/* * seach-sort.c * * This program performs timings of insertion sort, linear search, * binary search, and qsort. */ #include #include #include /* * Insertion sort */ void insertion_sort (int v[], int n) { int i, j, k; int a; /* for each element from 0 through n-2, swap it with the * smallest element from i through n-2 */ for (i=0; i bottom) { /* find the middle element */ middle = (bottom + top) / 2; if (v[middle] < value) /* value is in "upper" half */ bottom = middle; else if (v[middle] > value) /* value is in "lower" half */ top = middle; else return middle; /* found it */ } return -1; /* no luck, return -1 */ } /* Integer comparison function for qsort (defined in stdlib.h/libc) */ int intcmp (int *a, int *b) { return (*a < *b) ? -1 : (*a > *b) ? 1 : 0; } int main (int argc, char *argv[]) { int *v, n, i, j, c1, c2; float secs; if (argc != 2) { fprintf (stderr, "Usage: %s \n", argv[0]); exit (1); } n = atoi (argv[1]); v = (int *) malloc (n * sizeof (int)); /* fill array with random numbers */ for (i=0; i