UMBC CS 201, Fall 05
UMBC CMSC 201 Fall '05
CSEE | 201 | 201 F'05 | 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 300 million entries (approximate population of the United States), we would only need to examine 29 entries to find the position of any number.

The Program

/********************************************** ** File: search.c ** Author: R. Chang ** Date: ? ** Modified by: Sue Evans ** Date: 2/29/04 ** Section: 101 ** 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 array[], int maxSize) ; void SelectionSort (int array[], int size) ; int FindSmallest (int array[], int start, int stop) ; int FindNumber (int array[], int size, int number); int main() { int array[MAX], items, i, number ; /* get values */ printf("Enter items, one per line.\n") ; printf("End with sentinel: %d\n", SENTINEL) ; items = ReadArray (array, MAX) ; if (items == -1) { printf("Too many items!!!\n") ; } else { /* sort the array in place and print it */ SelectionSort(array, items) ; printf("\nSorted list:\n") ; for(i = 0 ; i < items ; i++) { printf("array[%d] = %d\n", i, array[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 = FindNumber(array, items, number) ; printf("%d is number %d on the list\n", array[i], i + 1) ; return 0; } /********************************************** ** Function: FindNumber () ** ** FindNumber() implements the Binary Search ** algorithm to locate the value in a sorted array ** ** 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 ** ** Assumptions: this simple Binary Search code assumes ** the value will be found ***********************************************/ int FindNumber(int array[], int size, int number) { int left, right, middle ; left = 0 ; right = size - 1 ; /* as long as there is still some portion */ /* of the array to be searched */ while (left <= right) { middle = (left + right) / 2 ; /* value was found so return its index */ if (number == array[middle]) { return(middle) ; } /* adjust right end of the part of the */ /* array being searched, so it is now */ /* only using the first half of the */ /* previous portion being searched */ if (number < array[middle]) { right = middle - 1 ; } /* adjust left end of the part of the */ /* array being searched, so it is now */ /* only using the last half of the */ /* previous portion being searched */ else { left = middle + 1 ; } } } /********************************************* ** Function: SelectionSort() ** ** Selection sort selects the smallest value ** in the unsorted portion of the array and ** moves it into the current position. The ** values "swap" positions. * ** 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 array[], int size) { int unsorted, smallestIndex, temp ; for (unsorted = 0; unsorted < size; unsorted++) { /* get the index of the smallest value */ /* in the unsorted part of the array */ smallestIndex = FindSmallest(array, unsorted, size); /* swap values */ temp = array[smallestIndex] ; array[smallestIndex] = array[unsorted] ; array[unsorted] = temp ; } } /********************************************* ** Function: FindSmallest() ** ** FindSmallest finds the smallest integer in the ** array between the index, start, and the index, ** stop, inclusive, and returns its index. ** ** 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 index corresponding to ** the smallest value **********************************************/ int FindSmallest(int array[], int start, int stop) { int smallestValue, smallestIndex, i ; smallestIndex = start ; smallestValue = array[start] ; for (i = start + 1 ; i < stop ; i++) { /* look for the smallest value in the */ /* unsorted part of the array */ if (array[i] < smallestValue) { smallestIndex = i ; smallestValue = array[i] ; } } return (smallestIndex) ; } /********************************************* ** Function: ReadArray() ** ** ReadArray() gets integer values and stores ** them in the array. It also counts the number ** of values read and will return the number of ** elements stored or a -1 if too many values ** were read. ** ** Input: an array of ints in which to store values ** the size of the array ** Output: returns the number of elements stored in ** the array or returns -1 if too many ** values are read **********************************************/ int ReadArray(int array[], int maxSize) { int i, value ; i = 0 ; /* The priming read */ scanf ("%d", &value); while (value != SENTINEL) { if (i == maxSize) { return(-1) ; } array[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 - Sunday, 25-Sep-2005 08:57:06 EDT


CSEE | 201 | 201 F'05 | lectures | news | help