Derive best, average and worst-case time and space complexity for supplied code, with line-level bottlenecks, stated assumptions and an operation-count check.
You are reviewing the complexity of one algorithm supplied as source code. Derive its time and space complexity from the code itself. Do not claim to have executed the code, and do not report exact operation counts unless they follow from a stated input.
## Inputs
Source code and language: [CODE]
What the input size variables mean (for example n = list length, V/E = vertices/edges): [SIZE_VARIABLES]
Input distribution or case of interest, if known: [INPUT_ASSUMPTIONS]
Library calls whose cost should be assumed (for example sort = O(n log n)): [LIBRARY_COSTS]
## Instructions
1. Name the algorithm or describe it in one sentence if it has no standard name.
2. Define every size variable before using it. Use more than one variable when the cost depends on more than one input.
3. Derive best-, average- and worst-case time by walking through the loops, recursion and library calls. Name the input that produces each case. If the average case depends on an input distribution that was not supplied, say so instead of guessing.
4. Report auxiliary space separately from input/output space, including recursion stack depth and any copies made by slicing or concatenation.
5. Identify bottlenecks by line or construct and state what each costs.
6. Suggest optimizations only when the asymptotic or constant-factor change is explained, and state any tradeoff (extra memory, preprocessing, changed ordering, stability).
7. Propose an empirical check: count a named operation at two or three input sizes so the reader can confirm the growth rate.
## Return
A JSON object with these fields:
- `algorithm_name`
- `size_variables`: object mapping each variable to its meaning
- `time_complexity`: `best_case`, `average_case`, `worst_case`, each with `bound`, `triggering_input` and `derivation`
- `space_complexity`: `auxiliary`, `recursion_depth`, `notes`
- `bottlenecks`: list of `{location, construct, cost}`
- `optimization_suggestions`: list of `{change, resulting_complexity, tradeoff}`
- `empirical_check`: the operation to count, the input sizes, and the counts the derivation predicts
- `assumptions` and `unresolved_questions`
## Acceptance checks
- Every bound uses only variables defined in `size_variables`.
- Best/worst cases match the code as written; for example, bubble sort is O(n) in the best case only if it stops early when a pass makes no swaps.
- Space analysis covers recursion depth and copies, not "memory leaks", which are a correctness issue rather than a complexity property.
- Predicted counts in `empirical_check` are arithmetic consequences of the derivation, not claimed measurements.Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Python:
def bubble_sort(a):
n = len(a)
for i in range(n - 1):
swapped = False
for j in range(n - 1 - i):
if a[j] > a[j + 1]:
a[j], a[j + 1] = a[j + 1], a[j]
swapped = True
if not swapped:
break
return a
Size variable: n = len(a). Case of interest: already-sorted and reverse-sorted lists.A JSON report (illustrative; not a recorded model run) in which: best case is O(n) with an already-sorted list because the swapped flag ends after one pass of n-1 comparisons; worst case is O(n^2) with a reverse-sorted list and n(n-1)/2 comparisons; auxiliary space is O(1) because the sort is in place and not recursive; the bottleneck is the inner comparison loop; the empirical_check predicts 9 and 45 comparisons for sorted and reversed lists of 10 items, and 99 and 4950 for 100 items.
Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts: