Big O Notation

Understanding algorithmic complexity and performance analysis.

Common Time Complexities

1. O(1) - Constant Time

// O(1) example - array access
func Get_First(arr []int) int {
    if len(arr) == 0 {
        return -1
    }
    return arr[0]
}

2. O(log n) - Logarithmic Time

// O(log n) example - binary search
func Binary_Search(arr []int, target int) int {
    left, right := 0, len(arr)-1
    for left <= right {
        mid := (left + right) / 2
        if arr[mid] == target {
            return mid
        }
        if arr[mid] < target {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    return -1
}

3. O(n) - Linear Time

4. O(n log n) - Linearithmic Time

5. O(n²) - Quadratic Time

Space Complexity Examples

1. O(1) Space

2. O(n) Space

Resources

Books

  1. "Introduction to Algorithms" by CLRS

  2. "Grokking Algorithms" by Aditya Bhargava

  3. "Data Structures and Algorithms in Go" by Hemant Jain

Online Resources

Video Tutorials

Practice Platforms

Last updated

Was this helpful?