Complexity Table

Complexity Table

Structured time and space complexity table for algorithm documentation

Installation

npx @ravikumarsurya/mdx-ui add complexity-table

Usage

import { ComplexityTable } from "@/components/mdx/complexity-table";
 
<ComplexityTable
  rows={[
    { operation: "Search", best: "O(1)", average: "O(log n)", worst: "O(n)" },
    {
      operation: "Insert",
      best: "O(log n)",
      average: "O(log n)",
      worst: "O(n)",
    },
  ]}
/>;

Examples

OperationBestAverageWorstSpace
SearchO(1)O(log n)O(n)O(1)
InsertO(1)O(log n)O(n)O(1)
DeleteO(1)O(log n)O(n)O(1)
TraverseO(n)O(n)O(n)O(n)

Binary Search Tree — time and space complexity

BST operations

<ComplexityTable
  rows={[
    { operation: "Search", best: "O(1)", average: "O(log n)", worst: "O(n)" },
    {
      operation: "Insert",
      best: "O(log n)",
      average: "O(log n)",
      worst: "O(n)",
    },
    {
      operation: "Delete",
      best: "O(log n)",
      average: "O(log n)",
      worst: "O(n)",
    },
  ]}
  caption="Binary Search Tree — unbalanced worst case"
/>

With space complexity

<ComplexityTable
  rows={[
    {
      operation: "Merge Sort",
      best: "O(n log n)",
      average: "O(n log n)",
      worst: "O(n log n)",
      space: "O(n)",
    },
    {
      operation: "Quick Sort",
      best: "O(n log n)",
      average: "O(n log n)",
      worst: "O(n²)",
      space: "O(log n)",
    },
    {
      operation: "Heap Sort",
      best: "O(n log n)",
      average: "O(n log n)",
      worst: "O(n log n)",
      space: "O(1)",
    },
  ]}
/>

Sparse columns

Only columns that have data in at least one row are rendered. Missing values show as —.

<ComplexityTable
  rows={[
    { operation: "Lookup", worst: "O(1)" },
    { operation: "Resize", worst: "O(n)" },
  ]}
/>

Props

ComplexityTable

PropTypeDefaultDescription
rowsComplexityRow[]Array of operation rows (required)
captionstringOptional caption below the table
classNamestringAdditional CSS classes

ComplexityRow

FieldTypeDescription
operationstringOperation name (required)
beststringBest-case complexity
averagestringAverage-case complexity
worststringWorst-case complexity
spacestringSpace complexity