LOOK Elevator Algorithm in Java — Definition, Working and Program
The LOOK Elevator Algorithm is an optimized scheduling algorithm used in elevator systems and operating systems (disk scheduling). It improves upon the SCAN algorithm by eliminating unnecessary movement.
Instead of moving all the way to the top or bottom floor, the elevator looks ahead and reverses direction as soon as the last requested floor is served.
What Is the LOOK Elevator Algorithm?
The LOOK algorithm works as follows:
- The elevator moves in one direction (UP or DOWN).
- It serves all floor requests in that direction.
- When there are no more requests ahead, it reverses direction.
- It does not go to the building’s extreme floors unless requested.
This makes LOOK more efficient than SCAN in real-life scenarios.
Why LOOK Is Better Than SCAN
- Reduces unnecessary elevator movement
- Saves time and energy
- More realistic for modern buildings
- Improves average response time
Example Scenario
Current Floor: 5
Direction: UP
Requested Floors: 1, 3, 4, 7, 9
How LOOK Works
- Move UP → serve 7
- Move UP → serve 9 (last request in UP direction)
- Reverse direction
- Move DOWN → serve 4 → 3 → 1
Service Order: 7 → 9 → 4 → 3 → 1
Java Program — LOOK Elevator Algorithm (Single Elevator)
import java.util.*;
public class LookElevator {
public static void main(String[] args) {
int currentFloor = 5;
String direction = "UP";
List requests = Arrays.asList(1, 3, 4, 7, 9);
List up = new ArrayList<>();
List down = new ArrayList<>();
for (int floor : requests) {
if (floor > currentFloor) {
up.add(floor);
} else if (floor < currentFloor) {
down.add(floor);
}
}
Collections.sort(up);
Collections.sort(down, Collections.reverseOrder());
System.out.println("LOOK Elevator Service Order:");
if (direction.equals("UP")) {
for (int floor : up) {
System.out.println("Serving floor " + floor);
}
for (int floor : down) {
System.out.println("Serving floor " + floor);
}
} else {
for (int floor : down) {
System.out.println("Serving floor " + floor);
}
for (int floor : up) {
System.out.println("Serving floor " + floor);
}
}
}
}
Sample Output
LOOK Elevator Service Order:
Serving floor 7
Serving floor 9
Serving floor 4
Serving floor 3
Serving floor 1
SCAN vs LOOK (Quick Comparison)
- SCAN: Goes to extreme floors even if not requested
- LOOK: Reverses at last requested floor
LOOK minimizes wasted movement and is closer to how real elevators behave.
Practice Challenges
- Modify the program to accept user input for floors and direction.
- Calculate total elevator movement (distance traveled).
- Extend the program to handle multiple elevators.
The LOOK Elevator Algorithm demonstrates how intelligent scheduling can significantly improve real-world systems like elevators and disk controllers.