<aside> ⚡ Big-O notation describes the upper bound of an algorithm's runtime as the input size grows

Untitled

</aside>

Linear Search

int linear_search(int a[], int n, int X) {
    for (int i = 0; i < n; i++) {
        if (a[i] == X) return i;
    }
    return -1;
}

Matrix Multiplication

void matrix_multiply(double a[N][N], double b[N][N], double c[N][N]) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            for (int k = 0; k < N; k++) {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }
}

Insertion Sort

void insertion_sort(int a[], int n) {
    for (int i = 0; i < n; i++) {
        for (int j = i; j > 0 && a[j] < a[j-1]; j--) {
            int temp = a[j];
            a[j] = a[j-1];
            a[j-1] = temp;
        }
    }
}

Factorial Calculation

int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

Recursive Binary Search

bool binary_search(int a[], int first, int last, int key) {
    if (first > last) return false;
    int mid = (first + last) / 2;
    if (a[mid] == key) return true;
    else if (key < a[mid]) return binary_search(a, first, mid-1, key);
    else return binary_search(a, mid+1, last, key);
}

Quicksort

void quicksort(int a[], int begin, int end) {
    if (begin < end) {
        int pivotIndex = sortAndShuffle(a, begin, end);
        quicksort(a, begin, pivotIndex - 1);
        quicksort(a, pivotIndex + 1, end);
    }
}

int sortAndShuffle(int a[], int begin, int end) {
    int pivotValue = a[begin];
    int current = begin;
    for (int i = begin + 1; i <= end; i++) {
        if (a[i] <= pivotValue) {
            current++;
            int temp = a[current];
            a[current] = a[i];
            a[i] = temp;
        }
    }
    int temp = a[begin];
    a[begin] = a[current];
    a[current] = temp;
    return current;
}