/**********************************************************/ /* */ /* Author: Kevin Klenk */ /* Date: May 25, 1998 */ /* Class: CMPS012A - Spring 1998 */ /* Professor: Ira Pohl */ /* Description: This program sorts randomly-filled */ /* arrays of four sizes (10, 100, 1000 and */ /* 10000) using insertion sort and then */ /* does several binary lookups on those */ /* arrays. Originally, the program was */ /* also supposed to print timing */ /* information, but the clock() function */ /* in time.h proved to be too unreliable. */ /* */ /**********************************************************/ /* Required libraries */ #include #include #include /* Constants */ #define CLOCKS_PER_SEC 1000000 #define MAXSIZE 10000 #define RANGE 100 #define NUMLOOKUPSTOPRINT 20 #define NUMLOOKUPS 100 /* Function Prototypes */ void swap(int *a, int *b); int GetRandNum(); void FillArray(int array[], int size); void PrintArray(int array[], int size); void Swap(int *a, int *b); void InsertionSort(int data[], int size); int BinaryLookup(int data[], int size, int key, int* position); /* Returns a random integer from 0 to RANGE - 1. */ int GetRandNum() { return (rand() % RANGE); } /* Fills an array of size integers with random values. */ void FillArray(int array[], int size) { int i; srand(time(NULL)); for(i = 0; i < size; i++) array[i] = GetRandNum(); } /* Prints an array of size integers to stdout. */ void PrintArray(int array[], int size) { int i; printf("\t"); for (i = 0; i < size; i++) printf("%5d", array[i]); printf("\n"); } /* Swaps two integers. */ void Swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; } /* Sorts an array of size integers in increasing order using */ /* the insertion sort algorithm. */ void InsertionSort(int data[], int size) { int i, k; for(i = 0; i < size - 1; ++i) { for(k = i + 1; k > 0; --k) { if(data[k] < data[k-1]) Swap(&data[k], &data[k-1]); } } } /* Looks for an integer, "key", in an array of size integers */ /* and sets the integer pointed to by position to the index */ /* which was last checked by the function. Returns 0 if */ /* "key" is not found; returns 1, otherwise. "position" */ /* points to an integer storing the index at which "key" is */ /* found if it IS in fact found. Otherwise, the integer */ /* pointed to by "position" will contain an index close */ /* to the index where "key" would have been found if it */ /* were in the array. */ int BinaryLookup(int data[], int size, int key, int* position) { int leftIndex = 0; int rightIndex = size - 1; *position = (leftIndex + rightIndex)/2; while ((leftIndex <= rightIndex) && (key != data[*position])) { if (key < data[*position]) { leftIndex = leftIndex; rightIndex = *position - 1; *position = (leftIndex + rightIndex)/2; } else if (key > data[*position]) { leftIndex = *position + 1; rightIndex = rightIndex; *position = (leftIndex + rightIndex)/2; } else { /* This will never occur because it is prevented by the loop */ /* condition. */ } } if (leftIndex > rightIndex) return 0; else return 1; } /* Main */ int main(void) { int currSize, i, r, found, position, numLookups; int data[MAXSIZE]; long before, after; for (currSize = 10; currSize <= MAXSIZE; currSize *= 10) { printf("\nRUNNING WITH %d ELEMENTS \n\n", currSize); FillArray(data, currSize); if (currSize == 10) { printf(" Before sorting array is:\n"); PrintArray(data, currSize); } /* before = clock(); */ printf(" Sorting the array:\n"); InsertionSort(data, currSize); /* after = clock(); printf("\n Sorting takes %lf seconds. \n\n", ((double)(after - before))/CLOCKS_PER_SEC); */ if (currSize == 10) { printf(" After sorting array is:\n"); PrintArray(data, currSize); } /* before = clock(); */ if (currSize == 10) numLookups = NUMLOOKUPSTOPRINT; else numLookups = NUMLOOKUPS; printf(" Doing %d binary lookups:\n", numLookups); for (i = 0; i < numLookups; i++) { r = GetRandNum(); found = BinaryLookup(data, currSize, r, &position); if (currSize == 10) { if (found) printf(" element = %2d found at index = %d\n", r, position); else printf(" element = %2d not found; would be near index = %d\n", r, position); } } /* after = clock(); printf("\n Doing %d lookups takes %lf seconds. \n\n", NUMLOOKUPS, ((double)(after - before))/CLOCKS_PER_SEC); */ } return 0; }