UMBC CS 201, Spring 02
UMBC CMSC 201 Spring '02 CSEE | 201 | 201 S'02 | lectures | news | help

search

The Task

With a sorted list, we can find the location of a number more efficiently than looking at every entry.

This program uses binary search to find the location of a number in a sorted list. In a list of 63 numbers, we need to examine at most 6 entries to find the location of any number.

If our array had 250 million entries (approximate population of the United States), we would only need to examine 38 entries to find the position of any number.

The Program

/********************************************** ** File: search.c ** Author: S. Bogar ** Date: 8/25/99 ** Section: 101 ** SSN: 123-45-6789 ** EMail: bogar@cs.umbc.edu ** ** Sample program for searching for a number ** in a sorted list. **********************************************/ #include <stdio.h> #define SENTINEL -1 #define MAX 100 /* function prototypes */ int ReadArray (int a[], int maxSize) ; void SelectionSort (int a[], int size) ; int FindSmallest (int a[], int start, int stop) ; int Find (int a[], int size, int number); int main() { int a[MAX], items, i, number ; /* get values from the user */ printf("Enter items, one per line.\n") ; printf("End with sentinel: %d\n", SENTINEL) ; items = ReadArray (a, MAX) ; if (items == -1) { printf("Too many items!!!\n") ; } else { /* sort the array in place and print it */ SelectionSort(a, items) ; printf("\nSorted list:\n") ; for(i = 0 ; i < items ; i++) { printf("a[%d] = %d\n", i, a[i]) ; } } /* ask user for value to find in the list */ printf("\nEnter number to find:\n" ) ; scanf ("%d", &number); /* find the number and display its position */ i = Find(a, items, number) ; printf("%d is number %d on the list\n", a[i], i+1) ; return 0; } /********************************************** ** Function: Find ** Input: a SORTED array of integers ** the size of the array ** a value to find in the array ** Output: the index in the array where the value was ** found ** ** Description: ** Find implements the Binary Search algorithm to ** locate the value ** NOTE: this simple Binary Search code assumes the ** value will be found ***********************************************/ int Find(int a[], int size, int number) { int left, right, middle ; left = 0 ; right = size - 1 ; while (left <= right) { middle = (left + right) / 2 ; if (number == a[middle]) { return(middle) ; } if (number < a[middle]) { right = middle - 1 ; } else { left = middle + 1 ; } } } /********************************************* ** Function: SelectionSort ** Input: an array of ints to sort ** the size of the array ** Output: the array is sorted "in place" ** there is no return value *********************************************/ void SelectionSort(int a[], int size) { int unsorted, smallestIndex, temp ; for (unsorted = 0; unsorted < size; unsorted++) { smallestIndex = FindSmallest(a, unsorted, size); /* swap values */ temp = a[smallestIndex] ; a[smallestIndex] = a[unsorted] ; a[unsorted] = temp ; } } /********************************************* ** Function: FindSmallest ** Input: an array of ints to search ** the 'start'ing and 'stop'ping indices in ** the array between which to search for ** the smallest ** Output: returns the smallest value ** between a[start] and a[stop - 1] **********************************************/ int FindSmallest(int a[], int start, int stop) { int smallestValue, smallestIndex, i ; smallestIndex = start ; smallestValue = a[start] ; for (i = start+1 ; i < stop ; i++) { if (a[i] < smallestValue) { smallestIndex = i ; smallestValue = a[i] ; } } return (smallestIndex) ; } /********************************************* ** Function: ReadArray ** Input: an array of ints in which to store values ** the size of the array ** Output: values are stored the array ** returns the number of elements stored in ** the array or returns -1 if too many ** values are read **********************************************/ int ReadArray(int a[], int maxSize) { int i, value ; i = 0 ; /* The priming read */ scanf ("%d", &value); while (value != SENTINEL) { if (i == maxSize) { return(-1) ; } a[i] = value ; i++ ; scanf ("%d", &value); } return (i) ; }

The Sample Run

linux1[78] % a.out Enter items, one per line. End with sentinel: -1 10 8 6 2 7 1 3 -1 Sorted list: a[0] = 1 a[1] = 2 a[2] = 3 a[3] = 6 a[4] = 7 a[5] = 8 a[6] = 10 Enter number to find: 7 7 is number 5 on the list lassie% linux1[79] % a.out < data2 Enter items, one per line. End with sentinel: -1 Sorted list: a[0] = 0 a[1] = 1 a[2] = 1 a[3] = 1 a[4] = 2 a[5] = 3 a[6] = 4 a[7] = 4 a[8] = 6 a[9] = 7 a[10] = 7 a[11] = 8 a[12] = 8 a[13] = 9 a[14] = 10 a[15] = 12 a[16] = 13 a[17] = 13 a[18] = 15 a[19] = 15 a[20] = 17 a[21] = 18 a[22] = 18 a[23] = 23 a[24] = 24 a[25] = 25 a[26] = 26 a[27] = 27 a[28] = 29 a[29] = 30 a[30] = 31 a[31] = 32 a[32] = 33 a[33] = 36 a[34] = 36 a[35] = 37 a[36] = 40 a[37] = 43 a[38] = 50 a[39] = 51 a[40] = 52 a[41] = 52 a[42] = 53 a[43] = 53 a[44] = 58 a[45] = 58 a[46] = 60 a[47] = 66 a[48] = 71 a[49] = 72 a[50] = 75 a[51] = 77 a[52] = 77 a[53] = 78 a[54] = 79 a[55] = 88 a[56] = 88 a[57] = 90 a[58] = 92 a[59] = 94 a[60] = 96 a[61] = 96 a[62] = 99 Enter number to find: 18 is number 22 on the list linux1[80] %
Last Modified - Thursday, 17-Jan-2002 13:52:11 EST


CSEE | 201 | 201 S'02 | lectures | news | help