Remarks
- A sequential search starts at the bginning of the list, and checks each item in the list until it finds a match or until it reaches the end of the list.
- A sequential search has one loops, which iterates to the size of the list. This makes the algorithm O(n); that is, maximum execution time is proportional to the list length.
- If the list doubles in length, average execution time is multiplied by 2.
- A sequential search is acceptable for short lists, but for long lists, there are better searching algorithms.
Example
/************************************************* * File: SequentialSearch.cpp * Description: A short program using * sequential search * Date: 11/3/08 *************************************************/ #include <iostream> #include <cmath> #include <cstring> using namespace std; //void Display (int List[], int Size); //Precondition: // List is an array of at least Size integers //Postcondition: // The first Size integers in the List // are displayed, separated by blanks. // char * Ordinal (int Index, char * Text); //Precondition: // Index is a non-negative integer // Text is a character string long enough to hold the result //Postcondition: // Text and the return value is the ordinal string // corresponding to the index: e.g., 0 => 1st, 5 => 6th // void Populate (int List[], int Size); //Precondition: // List is an array of at least Size integers //Postcondition: // The first Size integers in the List // are populated with random integers // int SequentialSearch (int Goal, int List[], int Size); //Precondition: // List is an array of at least Size integers //Postcondition: // If Goal is in the list, the return value is the // location (0..Size-1)of Goal in the list. // If Goal is not in the list, the return value is -1 // int main () { const int SIZE = 10; int Items [SIZE]; int Goal; int Location; char Ord [16]; char Response; cout << "Sequential Search Example\n"; Populate (Items, SIZE); cout << "The List:\n"; Display (Items, SIZE); do { cout << "Number to search for? "; cin >> Goal; Location = Search (Goal, Items, SIZE); if (Location < 0) { cout << Goal << " is not in the list.\n"; } else { cout << Goal << " is in the " << Ordinal (Location, Ord) << " Location " << endl; } cout << "Do you want to search again (Y/N)? "; cin >> Response; } while (toupper (Response) == 'Y'); return 0; } // void Display (int List[], int Size) { int i; i = 0; while (i < Size) { cout << List [i] << ' '; ++i; } cout << endl; } // char * Ordinal (int i, char * Ord) { static const char * Suffix [] = {"st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th"}; sprintf (Ord, "%d", i + 1); strcat (Ord, Suffix [i % 10]); return Ord; } // void Populate (int List[], int Size) { int i; i = 0; randomize (); while (i < Size) { List [i] = 1 + rand() % 99; ++i; } } // int SequentialSearch (int Goal, int List[], int Size) { int i; i = 0; while ((i < Size) && (List [i] != Goal)) { ++i; } if (i == Size) { i = -1; } return i; } //Output
Sequential Search Example The List: 11 8 39 1 2 1 12 40 80 62 Number to search for? 44 44 is not in the list. Do you want to search again (Y/N)? y Number to search for? 11 11 is in the 1st Location Do you want to search again (Y/N)? n