Courses / Physics I
Computer Science

Searching and sorting ideas

Physics I 293 words Free to read

Finding and ordering data efficiently is fundamental to computational physics — from locating a particle in a grid to ranking eigenvalues.

Linear search — Check every element one by one. Cost: O(n)O(n).

def linear_search(arr, target):
    for i, val in enumerate(arr):
        if val == target:
            return i
    return -1

Binary search — Requires a sorted array. Repeatedly halve the search space. Cost: O(logn)O(\log n).

def binary_search(arr, target):
    lo, hi = 0, len(arr) - 1
    while lo <= hi:
        mid = (lo + hi) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            lo = mid + 1
        else:
            hi = mid - 1
    return -1

Sorting algorithms

AlgorithmAverage costStable?
Bubble sortO(n2)O(n^{2})Yes
Merge sortO(nlogn)O(n\log n)Yes
Quick sortO(nlogn)O(n\log n)No

In practice, use Python's built-in sorted() or np.sort(), which use highly optimised O(nlogn)O(n\log n) algorithms.

Key insight: Binary search is O(logn)O(\log n) — searching 1 billion sorted elements takes only about 30 comparisons. But the data must be sorted first.
Common pitfall: Binary search demands sorted input — on unsorted data it fails silently, confidently returning wrong answers. Fast algorithms usually buy their speed with preconditions; know what you promised.

Algorithmic Complexity

The efficiency of a sorting algorithm is measured by comparisons as a function of input size nn.

The lower bound for comparison sorting is Ω(nlogn)\Omega(n \log n) — no comparison-based algorithm can do better.

Choosing the right algorithm can mean the difference between seconds and hours on large datasets.
Sorting Algorithm Visualization

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

14practice questions
2interactive scenes
Start Physics I free

Computer Science