Sequential Search and Selection Sort Analysis
Sequential Search Algorithm and Analysis
Definition and Purpose: The sequential search algorithm is an algorithm used to search a list of items to locate a specific "target" item. It follows a repetitive task of comparing the target item against successive numbers or names in a list until the item is found or the list is exhausted.
Formal Algorithm Steps:
Get values for , , and .
Set the value of to 1 and set the value of to .
While () and () do Steps 4 through 7.
If is equal to the number on the list, , then
Print the name of the corresponding person, .
Set the value of to .
Else ( is not equal to ), add 1 to the value of .
If () then
Print the message 'Sorry, this number is not in our directory'.
Stop.
Central Unit of Work: The primary unit of work in sequential search is the comparison of the being searched for against an individual element in the list.
Peripheral Tasks: Tasks such as setting initial values for the index () and markers (), writing output, and moving the index forward are considered peripheral. These are ignored in high-level efficiency analysis because their contribution to the total work is constant or negligible compared to the repetitive comparison process.
Analysis of Comparisons:
Best Case: Occurs if the is the very first item in the list. This requires only 1 comparison.
Worst Case: Occurs if the is the very last item in the list or is not present at all. In either case, the target must be compared against all items, requiring comparisons.
Average Case: If the target occurs randomly in the list, the average number of comparisons is approximately .
Real-World Application Example: In a reverse directory with entries, if a computer performs comparisons per second:
Average Time: (nearly one hour).
Worst Case Time: Approximately 2 hours to determine a number is not in the directory.
Space Efficiency: Sequential search is highly space efficient. Aside from storing the list and the target, it only requires two additional memory locations (one for the index and one for the indicator).
Order of Magnitude and Order n ()
Concept of Order of Magnitude: This concept explains why constant factors (like peripheral work) are less important than the rate of growth. If is a constant factor for peripheral work, total work is expressed as .
Comparison of Growth Rates:
If , work grows at twice the rate of .
If , work grows at the same rate as .
If , work grows at half the rate of .
Definition of : Any algorithm where the amount of work varies as a constant times is said to be of order of magnitude , written as . Sequential search is an algorithm in both the worst and average cases.
Selection Sort: Algorithm and Execution
Definition: Selection sort is a sorting algorithm that organizes a list into ascending or alphabetical order by "growing" a sorted subsection from the back of the list to the front.
Algorithm Steps:
Get values for and the list items.
Set the marker for the unsorted section at the end of the list.
While the unsorted section of the list is not empty, do Steps 4 through 6:
Select the largest number in the unsorted section of the list.
Exchange this number with the last number in the unsorted section of the list.
Move the marker for the unsorted section left one position.
Stop.
Example Trace (List: 5, 7, 2, 8, 3):
Initial: (Marker at the end; sorted section empty).
Find largest (8), swap with last unsorted (3), move marker: .
Find largest in unsorted (7), swap with last unsorted (3), move marker: .
Find largest in unsorted (5), swap with last unsorted (2), move marker: .
Find largest in unsorted (3), swap with itself, move marker: .
Single number left (2) is largest, move marker: .
Mechanics of Exchanging Values
Requirement of Temporary Storage: Exchanging two values at positions and requires a third temporary location (). A direct copy ( then ) fails because the original value of is overwritten and lost.
Correct Exchange Algorithm:
Copy the current value at position into location .
Copy the current value at position into position .
Copy the current value at location into position .
Analysis of Selection Sort Efficiency
Unit of Work: The unit of work is the comparison performed during the "Find Largest" subtask. To find the largest in a list of size , the first number is used as "largest-so-far" and compared to the remaining items.
Total Comparisons: The number of comparisons decreases as the unsorted section shrinks:
Mathematical Formula: Based on Gauss\'s sum, the total comparisons are:
Performance Scaling: | Length of List | | Number of Comparisons Required | | :--- | :--- | :--- | | 10 | 100 | 45 | | 100 | 10,000 | 4,950 | | 1,000 | 1,000,000 | 499,500 |
Work Invariance: Selection sort does the same amount of work regardless of the initial arrangement of numbers. It cannot recognize if a list is already sorted; it will still perform the full sequence of comparisons and exchanges.
Exchanges: The algorithm performs exactly exchanges (one for each position in the list), even if a number is exchanged with itself.
Space Efficiency: It is space efficient, requiring only a few extra memory locations for the iteration marker, the "largest-so-far" tracker, and the temporary storage used for exchanges.
Practice Problems & Case Studies
Sorting Examples:
List (4, 8, 2, 6): After exchange 1: (4, 6, 2, 8). After exchange 2: (4, 2, 6, 8). After exchange 3: (2, 4, 6, 8).
List (12, 3, 6, 8, 2, 5, 7): Becomes (7, 3, 6, 5, 2, 8, 12) (2, 3, 6, 5, 7, 8, 12) (2, 3, 5, 6, 7, 8, 12).
Already Sorted List (3, 7, 12, 16, 21): The largest element is always at the back of the unsorted section. It exchanges with itself, resulting in no visual change, though operations are still performed.
The Pancake Problem:
Posed by Jacob Goodman (1975). A waiter flips a stack of varying sized pancakes using a spatula to order them from largest (bottom) to smallest (top).
The maximum number of flips needed for pancakes is the pancake number ().
Standard algorithm flipping logic: .
William Gates and Christos Papadimitriou (1979) published an improved algorithm in Discrete Mathematics requiring at most flips.