Elevator algorithm. The elevator algorithm, or SCAN, is a disk-scheduling algorithm to determine the motion of the disk's arm and head in servicing read and write requests.

This algorithm is named after the behavior of a building elevator, where the elevator continues to travel in its current direction (up or down) until empty, stopping only to let individuals off or to pick up new individuals heading in the same direction.  

From an implementation perspective, the drive maintains a buffer of pending read/write requests, along with the associated cylinder number of the request, in which lower cylinder numbers generally indicate that the cylinder is closer to the spindle, and higher numbers indicate the cylinder is farther away.

Description 

When a new request arrives while the drive is idle, the initial arm/head movement will be in the direction of the cylinder where the data is stored, either in or out.  As additional requests arrive, requests are serviced only in the current direction of arm movement until the arm reaches the edge of the disk. When this happens, the direction of the arm reverses, and the requests that were remaining in the opposite direction are serviced, and so on.

Variations 
One variation of this method ensures all requests are serviced in only one direction, that is, once the head has arrived at the outer edge of the disk, it returns to the beginning and services the new requests in this one direction only (or vice versa). This is known as the "Circular Elevator Algorithm" or C-SCAN.  Although the time of the return seek is wasted, this results in more equal performance for all head positions, as the expected distance from the head is always half the maximum distance, unlike in the standard elevator algorithm where cylinders in the middle will be serviced as much as twice as often as the innermost or outermost cylinders.

Other variations include:
 FSCAN
 LOOK
 C-LOOK
 N-Step-SCAN

Example 
The following is an example of how to calculate average disk seek times for both the SCAN and C-SCAN algorithms.

 Example list of pending disk requests (listed by track number): 100, 50, 10, 20, 75.
 The starting track number for the examples will be 35.
 The list will need to be sorted in ascending order: 10, 20, 50, 75, 100.

Both SCAN and C-SCAN behave in the same manner until they reach the last track queued. For the sake of this example let us assume that the SCAN algorithm is currently going from a lower track number to a higher track number (like the C-SCAN is doing). For both methods, one takes the difference in magnitude (i.e. absolute value) between the next track request and the current track.

 Seek 1: 50 − 35 = 15
  Seek 2: 75 − 50 = 25
  Seek 3: 100 − 75 = 25

At this point both have reached the highest (end) track request. SCAN will just reverse direction and service the next closest disk request (in this example, 20) and C-SCAN will always go back to track 0 and start going to higher track requests.

  Seek 4 (SCAN): 20 − 100 = 80
  Seek 5 (SCAN): 10 − 20 = 10
  Total (SCAN): 155
  Average (SCAN): 155 ÷ 5 = 31
  Seek 4 (C-SCAN): 0 − 100 = 0 head movement as cylinders are treated as a  circular list (C-SCAN always goes back to the first track)
  Seek 5 (C-SCAN): 10 − 0 = 10
  Seek 6 (C-SCAN): 20 − 10 = 10
  Total (C-SCAN): 85
  Average (C-SCAN): 85 ÷ 5 = 17

Even though six seeks were performed using the C-SCAN algorithm, only five I/Os were actually done.

The scan algorithm (often known as prefix sum algorithm) is commonly used in computer science for problems that involve computing the cumulative sum or the cumulative result of a series of elements in an array or list. It operates by iterating through the list and maintaining an accumulated result up to the current element, and can be used in various scenarios such as parallel computation, reducing operations, or processing large data efficiently.

Here’s a real-world example where the scan algorithm is applied:

Example: Real-Time Data Processing in Stock Market Analysis
Imagine a financial trading application that tracks the price changes of a stock over time and calculates the cumulative value or gain/loss of the stock at any given moment.

Scenario:
You have a list of daily stock price changes (positive or negative values) for a particular stock over a period of days. The scan algorithm can be used to calculate the cumulative gain or loss at each day, showing the total change in stock price from the start up to that day.

Input data: A list of daily stock price changes:

csharp
Copy code
[10, -5, 3, 7, -2]
Where:

10 represents a gain of $10 on day 1,
-5 represents a loss of $5 on day 2,
3 represents a gain of $3 on day 3,
7 represents a gain of $7 on day 4,
-2 represents a loss of $2 on day 5.
Step-by-Step Execution (Scan Algorithm):

Start with an initial cumulative sum of 0.
For each subsequent day, add the day's change to the cumulative sum:
Day 1: 0 + 10 = 10 (Total gain so far: $10)
Day 2: 10 + (-5) = 5 (Total gain so far: $5)
Day 3: 5 + 3 = 8 (Total gain so far: $8)
Day 4: 8 + 7 = 15 (Total gain so far: $15)
Day 5: 15 + (-2) = 13 (Total gain so far: $13)
Output: The cumulative gains after each day:

csharp
Copy code
[10, 5, 8, 15, 13]
This cumulative list shows how the stock has performed in terms of total gain/loss at each day in the period. By using the scan algorithm, you efficiently compute these cumulative results in a single pass over the data.

Parallelism and Optimization:
In a real-world application where the data set is large (e.g., tracking stock prices across thousands of days for multiple stocks), this scan algorithm can be parallelized to run faster. For example, using techniques like parallel prefix sum (where the input array is divided into chunks, processed in parallel, and then merged), large-scale data can be processed much more efficiently.

Use Cases Beyond Stock Market Analysis:
Image Processing: Cumulative operations (like pixel intensities) for tasks such as convolution or blur filters.
Distributed Systems: Aggregating values across multiple nodes to compute cumulative statistics.
Database Systems: Efficiently computing running totals in financial applications or analytics.
The scan algorithm is essential in scenarios where you need to process or aggregate data in a way that builds on prior computations, and its parallel implementation helps in scaling up for larger datasets.

Analysis 
For both versions of the elevator algorithm, the arm movement is less than twice the number of total cylinders and produces a smaller variance in response time. The algorithm is also relatively simple. 

The elevator algorithm is not always better than shortest seek first, which is slightly closer to optimal, but can result in high variance in response time and even in starvation when new requests continually get serviced prior to existing requests. Anti-starvation techniques can be applied to the shortest seek time first algorithm to guarantee a maximum response time.

See also
FIFO (computing and electronics)

References 

Disk scheduling algorithms
Sorting algorithms