Searching Data
Finding data efficiently is fundamental in computational physics, from grid tracking to sorting eigenvalues.
Linear search checks every element sequentially. Cost: .
Binary search repeatedly halves the search space. Cost: . For 1 billion sorted elements, it takes only about 30 comparisons.
| Algorithm | Time Cost | Precondition |
|---|---|---|
| Linear | None | |
| Binary | Sorted array |
Common pitfall: Binary search demands sorted input. On unsorted data, it fails silently and returns wrong answers. Fast algorithms buy speed with preconditions.
Sorting Algorithms
The efficiency of sorting is measured by comparisons relative to input size . The lower bound for comparison sorting is .
| Algorithm | Average Cost | Stable? |
|---|---|---|
| Bubble sort | Yes | |
| Merge sort | Yes | |
| Quick sort | No |
- algorithms compare every pair.
- algorithms use divide-and-conquer.
In practice, use Python's built-in sorted() or np.sort(), which use optimised algorithms.