A comprehensive tool for analyzing time and space complexity of Java code snippets through both console and GUI interfaces.
The Java Code Complexity Analyzer is an educational tool designed to help developers and students understand the performance characteristics of their Java code. It uses pattern recognition and static code analysis techniques to automatically detect common algorithmic patterns and provide instant feedback on time and space complexity.
This project offers two user interfaces:
- Console Version (
ComplexityAnalyzer.java) - Lightweight command-line interface for quick analysis - GUI Version (
ComplexityAnalyzerGUI.java) - User-friendly graphical interface with visual feedback
The tool analyzes code patterns including nested loops, recursion, sorting operations, data structure allocations, and divide-and-conquer patterns to estimate Big-O complexity.
-
Time Complexity Detection
- Identifies nested loops and calculates O(n), O(nยฒ), O(nยณ), etc.
- Detects sorting operations (Arrays.sort, Collections.sort) โ O(n log n)
- Recognizes recursion patterns โ O(2^n) or context-dependent
- Identifies binary search and divide-and-conquer patterns โ O(log n)
- Recognizes constant time operations โ O(1)
-
Space Complexity Analysis
- Detects array and collection allocations โ O(n)
- Identifies recursive calls using stack space โ O(n)
- Recognizes constant space usage โ O(1)
-
Console Interface
- Simple text-based input/output
- Type 'END' to complete code entry
- Immediate analysis results
- No GUI dependencies required
-
Graphical Interface
- Clean, minimalist design
- Large code editor with monospaced font
- Scrollable text area for long code snippets
- One-click analysis button
- Clear results display with explanations
- Input validation with error dialogs
- Multi-line code support
- Input validation and error handling
- Detailed explanations with complexity results
- Pattern-based heuristic analysis
- No compilation required - analyzes code as text
- Zero external dependencies
- Programming Language: Java
- JDK Version: Java 8 or higher
- GUI Framework: Java Swing (javax.swing)
- Pattern Matching: Java Regular Expressions (java.util.regex)
- I/O Handling: Java Scanner, BufferedReader
- Development Tools:
- Any Java IDE (IntelliJ IDEA, Eclipse, VS Code) or text editor
- Java Compiler (javac)
- Java Runtime Environment (JRE)
Key Libraries Used:
java.util.Scanner- Console input handlingjava.util.regex.Pattern- Pattern matching for code analysisjava.util.regex.Matcher- Regex operationsjavax.swing.*- GUI componentsjava.awt.*- Layout managers and UI controls
No External Dependencies - The project uses only standard JDK libraries, making it highly portable and easy to run on any Java-enabled system.
Ensure you have the following installed on your system:
- Java Development Kit (JDK) 8 or higher
- Check version:
java -version - Download from: Oracle JDK or OpenJDK
- Check version:
- Terminal/Command Prompt access
- Text Editor or IDE (optional but recommended)
-
Download the Source Files
# Clone the repository (if using Git) git clone <repository-url> cd java-complexity-analyzer # Or download the following files directly: # - ComplexityAnalyzer.java # - ComplexityAnalyzerGUI.java # - README.md # - statement.md
-
Compile the Programs
Open terminal/command prompt in the project directory and run:
# Compile console version javac ComplexityAnalyzer.java # Compile GUI version javac ComplexityAnalyzerGUI.java
This will generate
.classfiles in the same directory. -
Verify Compilation
Check that the following files exist:
ComplexityAnalyzer.classComplexityAnalyzerGUI.class
java ComplexityAnalyzerUsage:
- The program will prompt you to enter Java code
- Paste or type your code snippet
- Type
ENDon a new line when finished - Press Enter to see the analysis results
Example:
=== Java Code Complexity Analyzer ===
Enter your Java code (type 'END' on a new line to finish):
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.println(i + j);
}
}
END
=== Analysis Results ===
Time Complexity: O(n^2) - Nested loops detected
Space Complexity: O(1) - Constant space
java ComplexityAnalyzerGUIUsage:
- A window will open with the title "Java Code Complexity Analyzer"
- Enter or paste your Java code in the text area
- Click the "Analyze Complexity" button
- View results in the "Analysis Results" panel at the bottom
for (int i = 0; i < n; i++) {
System.out.println(i);
}Expected Output:
- Time Complexity: O(n) - Single loop detected
- Space Complexity: O(1) - Constant space
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.println(i + j);
}
}Expected Output:
- Time Complexity: O(n^2) - Nested loops detected
- Space Complexity: O(1) - Constant space
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}Expected Output:
- Time Complexity: O(log n) - Loop with division/halving detected
- Space Complexity: O(1) - Constant space
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n-1) + fibonacci(n-2);
}Expected Output:
- Time Complexity: O(2^n) or higher - Recursion detected (depends on structure)
- Space Complexity: O(n) - Recursion uses call stack space
int[] arr = new int[n];
Arrays.sort(arr);Expected Output:
- Time Complexity: O(n log n) - Sorting operation detected
- Space Complexity: O(n) - Data structure allocation detected
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
// operation
}
}
}Expected Output:
- Time Complexity: O(n^3) - 3 nested loops detected
- Space Complexity: O(1) - Constant space
int[] numbers = new int[n];
ArrayList<String> list = new ArrayList<>();Expected Output:
- Time Complexity: O(1) - Constant time
- Space Complexity: O(n) - Data structure allocation detected
- Console version compiles without errors
- GUI version compiles without errors
- Console version accepts multi-line input
- Console version recognizes 'END' command
- GUI window opens successfully
- GUI text area accepts input
- "Analyze Complexity" button is clickable
- Empty input shows error message (GUI)
- Single loop detected as O(n)
- Nested loops detected correctly (O(nยฒ), O(nยณ))
- Sorting operations identified as O(n log n)
- Recursion detected appropriately
- Binary search patterns recognized as O(log n)
- Space complexity correctly identifies arrays/collections
- Space complexity recognizes recursive stack usage
- Results display with clear explanations
- Both versions produce consistent results for same input
- Empty Input - Should show error message
- Single Statement - Should return O(1)
- Code with Comments - Should handle gracefully
- Very Long Code (500+ lines) - Should complete within 2 seconds
- Mixed Patterns - Code with loops + recursion + sorting
void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}Analysis Result: Time O(nยฒ), Space O(1)
void mergeSort(int[] arr, int l, int r) {
if (l < r) {
int m = (l + r) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}Analysis Result: Time O(log n) or O(n log n), Space O(n)
java-complexity-analyzer/
โ
โโโ ComplexityAnalyzer.java # Console version source code
โโโ ComplexityAnalyzerGUI.java # GUI version source code
โโโ README.md # This file
โโโ statement.md # Problem statement and scope
โ
โโโ ComplexityAnalyzer.class # Compiled console version (after compilation)
โโโ ComplexityAnalyzerGUI.class # Compiled GUI version (after compilation)
โ
โโโ docs/ # Additional documentation (optional)
โโโ Java Code Complexity Analyzer - Om Shrivastava.pdf # Detailed project report
This is an academic project for VIT Bhopal University. However, suggestions for improvements are welcome:
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Open a Pull Request
- Analysis is based on pattern matching, not full AST parsing
- May not detect all complexity patterns in highly optimized code
- Comments and strings containing keywords might affect accuracy
- Does not analyze library function complexities in detail
- Amortized complexity is not calculated
- Best/average/worst case distinctions are not made
- Integration with Java AST parser for more accurate analysis
- Support for analyzing multiple files at once
- Export results to PDF/CSV format
- Syntax highlighting in GUI
- More sophisticated recursion analysis
- Support for other programming languages
- IDE plugin development
- Real-time analysis as user types
This project is developed as part of academic coursework at VIT Bhopal University.
Course: Programming with Java (CSE2006)
Academic Year: 2025-26
School: School of Computing Science and Engineering (Cloud Computing)
Om Shrivastava
Registration Number: 24BSA10362
VIT Bhopal University
Email: [omshrivastava01927@gmail.com]
Email: [shrivastava.24bsa10362@vitbhopal.ac.in]
- VIT Bhopal University faculty for guidance
- Course materials and references from Introduction to Algorithms (CLRS)
- Java documentation and community resources
- Open-source community for inspiration
For issues, questions, or suggestions:
- Open an issue in the repository
- Contact via email
- Refer to the detailed project report for more information
Last Updated: November 23, 2025