O(n²) – Quadratic Time Complexity
After learning about O(n log n), let's move to another important type of Time Complexity called Quadratic Time Complexity, written as O(n²).
This is one of the most common time complexities that beginners encounter while learning programming and algorithms.
Many simple algorithms use O(n²) because they are easy to understand and implement. However, these algorithms become slow when the input size increases.
Let's understand why.
{tocify} $title={Table of Contents}
What is O(n²)?
O(n²) means the running time of an algorithm increases with the square of the input size.
In simple words, if the input size doubles, the number of operations becomes approximately four times larger.
If the input size becomes three times larger, the work becomes about nine times larger.
This rapid growth makes Quadratic algorithms inefficient for handling large datasets.
Simple Definition
Quadratic Time Complexity (O(n²)) means that for every input element, the algorithm performs another complete loop over all the elements.
Because of this, the total number of operations becomes:
n × n = n²
This is why it is called Quadratic Time Complexity.
Real-Life Example
Imagine you are organizing a classroom competition.
There are 10 students, and each student has to play a game with every other student.
Student 1 plays against all 10 students.
Student 2 also plays against all 10 students.
Student 3 repeats the same process.
This continues until every student has played with everyone.
If there are more students, the number of matches grows very quickly.
This is a perfect example of O(n²).
Why Does O(n²) Become Slow?
Suppose we compare every student with every other student.
| Students | Comparisons |
|---|---|
| 5 | 25 |
| 10 | 100 |
| 100 | 10,000 |
| 1,000 | 1,000,000 |
Notice how quickly the number of operations increases.
This is why Quadratic algorithms are not suitable for processing millions of records.
Understanding O(n²) with Nested Loops
The easiest way to identify O(n²) is by looking for nested loops.
Consider this simple C++ program.
#include <iostream>
using namespace std;
int main()
{
int n = 5;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
cout << "(" << i << "," << j << ") ";
}
cout << endl;
}
return 0;
}
Output
(1,1) (1,2) (1,3) (1,4) (1,5)
(2,1) (2,2) (2,3) (2,4) (2,5)
(3,1) (3,2) (3,3) (3,4) (3,5)
(4,1) (4,2) (4,3) (4,4) (4,5)
(5,1) (5,2) (5,3) (5,4) (5,5)
The outer loop runs 5 times.
For every iteration of the outer loop, the inner loop also runs 5 times.
Total operations:
5 × 5 = 25
If n = 100, then:
100 × 100 = 10,000 operations
Therefore,
Time Complexity = O(n²)
Visual Representation
Imagine a table.
1 2 3 4
1 ✔ ✔ ✔ ✔
2 ✔ ✔ ✔ ✔
3 ✔ ✔ ✔ ✔
4 ✔ ✔ ✔ ✔
Every row interacts with every column.
Rows = n
Columns = n
Total cells:
n × n = n²
This is exactly how Quadratic algorithms work.
Bubble Sort Example
One of the most famous algorithms with O(n²) complexity is Bubble Sort.
Bubble Sort repeatedly compares two adjacent elements and swaps them if they are in the wrong order.
C++ Program
#include <iostream>
using namespace std;
int main()
{
int arr[] = {5, 2, 4, 1, 3};
int n = 5;
for(int i = 0; i < n - 1; i++)
{
for(int j = 0; j < n - i - 1; j++)
{
if(arr[j] > arr[j + 1])
{
swap(arr[j], arr[j + 1]);
}
}
}
cout << "Sorted Array:" << endl;
for(int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
}
Output
Sorted Array
1 2 3 4 5
Bubble Sort uses two loops.
The outer loop controls the number of passes.
The inner loop compares elements.
Since both loops depend on n, Bubble Sort has:
O(n²)
Another Real-Life Example
Suppose you have 100 photos on your phone.
You want to compare every photo with every other photo to find duplicate images.
Photo 1 is compared with all photos.
Photo 2 is compared with all photos.
Photo 3 repeats the same process.
This continues until every photo has been checked.
As the number of photos increases, the total comparisons grow rapidly.
This is another example of Quadratic Time Complexity.
O(n²) Growth Table
| Input Size | Number of Operations |
|---|---|
| 10 | 100 |
| 20 | 400 |
| 50 | 2,500 |
| 100 | 10,000 |
| 500 | 250,000 |
| 1,000 | 1,000,000 |
Notice how increasing the input size slightly causes the number of operations to increase dramatically.
O(n) vs O(n log n) vs O(n²)
| Input Size | O(n) | O(n log n) | O(n²) |
|---|---|---|---|
| 100 | 100 | 664 | 10,000 |
| 1,000 | 1,000 | 9,966 | 1,000,000 |
| 10,000 | 10,000 | 132,877 | 100,000,000 |
As you can see, O(n²) becomes much slower than both O(n) and O(n log n) when the input size grows.
Where is O(n²) Used?
Although Quadratic algorithms are slower, they are still useful in some situations.
Common uses include:
- Bubble Sort
- Selection Sort
- Insertion Sort (Worst Case)
- Comparing every pair of elements
- Duplicate detection using nested loops
- Matrix operations
- Pattern matching problems
- Small datasets where simplicity is more important than speed
Advantages of O(n²)
- Easy to understand.
- Simple to implement.
- Good for beginners learning algorithms.
- Works well for small datasets.
- Helpful for educational purposes.
Disadvantages of O(n²)
- Very slow for large datasets.
- Uses many unnecessary comparisons.
- Not suitable for real-time applications.
- Modern software usually avoids Quadratic algorithms when faster options are available.
Interview Tip
If you notice two nested loops, don't immediately assume the algorithm is O(n²).
Ask yourself:
- Do both loops depend on n?
- Does the inner loop run for every iteration of the outer loop?
If the answer is yes, then the Time Complexity is usually O(n²).
However, some nested loops run fewer times, so always analyze the code carefully instead of relying only on the number of loops.
Quick Revision
- O(n²) is called Quadratic Time Complexity.
- It usually appears when an algorithm contains two nested loops.
- The running time grows as the square of the input size.
- Bubble Sort and Selection Sort are common examples.
- O(n²) works well for small datasets but becomes slow for large amounts of data.
- Whenever possible, developers prefer O(n log n) algorithms because they perform much better on larger inputs.
🧩 Algorithm
📘 IT Tech Language
☁️ Cloud Computing - What is Cloud Computing – Simple Guide
- History and Evolution of Cloud Computing
- Cloud Computing Service Models (IaaS)
- What is IaaS and Why It’s Important
- Platform as a Service (PaaS) – Cloud Magic
- Software as a Service (SaaS) – Enjoy Software Effortlessly
- Function as a Service (FaaS) – Serverless Explained
- Cloud Deployment Models Explained
🧩 Algorithm - Why We Learn Algorithm – Importance
- The Importance of Algorithms
- Characteristics of a Good Algorithm
- Algorithm Design Techniques – Brute Force
- Dynamic Programming – History & Key Ideas
- Understanding Dynamic Programming
- Optimal Substructure Explained
- Overlapping Subproblems in DP
- Dynamic Programming Tools
🤖 Artificial Intelligence (AI) - Artificial intelligence and its type
- Policy, Ethics and AI Governance
- How ChatGPT Actually Works
- Introduction to NLP and Its Importance
- Text Cleaning and Preprocessing
- Tokenization, Stemming & Lemmatization
- Understanding TF-IDF and Word2Vec
- Sentiment Analysis with NLTK
📊 Data Analyst - Why is Data Analysis Important?
- 7 Steps in Data Analysis
- Why Is Data Analysis Important?
- How Companies Can Use Customer Data and Analytics to Improve Market Segmentation
- Does Data Analytics Require Programming?
- Tools and Software for Data Analysis
- What Is the Process of Collecting Import Data?
- Data Exploration
- Drawing Insights from Data Analysis
- Applications of Data Analysis
- Types of Data Analysis
- Data Collection Methods
- Data Cleaning & Preprocessing
- Data Visualization Techniques
- Overview of Data Science Tools
- Regression Analysis Explained
- The Role of a Data Analyst
- Time Series Analysis
- Descriptive Analysis
- Diagnostic Analysis
- Predictive Analysis
- Pescriptive Analysis
- Structured Data in Data Analysis
- Semi-Structured Data & Data Types
- Can Nextool Assist with Data Analysis and Reporting?
- What Kind of Questions Are Asked in a Data Analyst Interview?
- Why Do We Use Tools Like Power BI and Tableau for Data Analysis?
- The Power of Data Analysis in Decision Making: Real-World Insights and Strategic Impact for Businesses
📊 Data Science - The History and Evolution of Data Science
- The Importance of Data in Science
- Why Need Data Science?
- Scope of Data Science
- How to Present Yourself as a Data Scientist?
- Why Do We Use Tools Like Power BI and Tableau
- Data Exploration: A Simple Guide to Understanding Your Data
- What Is the Process of Collecting Import Data?
- Understanding Data Types
- Overview of Data Science Tools and Techniques
- Statistical Concepts in Data Science
- Descriptive Statistics in Data Science
- Data Visualization Techniques in Data Science
- Data Cleaning and Preprocessing in Data Science
🧠 Machine Learning (ML) - How Machine Learning Powers Everyday Life
- Introduction to TensorFlow
- Introduction to NLP
- Text Cleaning and Preprocessing
- Sentiment Analysis with NLTK
- Understanding TF-IDF and Word2Vec
- Tokenization and Lemmatization
🗄️ SQL
💠 C++ Programming - Introduction of C++
- Brief History of C++ || History of C++
- Characteristics of C++
- Features of C++ || Why we use C++ || Concept of C++
- Interesting Facts About C++ || Top 10 Interesting Facts About C++
- Difference Between OOP and POP || Difference Between C and C++
- C++ Program Structure
- Tokens in C++
- Keywords in C++
- Constants in C++
- Basic Data Types and Variables in C++
- Modifiers in C++
- Comments in C++
- Input Output Operator in C++ || How to take user input in C++
- Taking User Input in C++ || User input in C++
- First Program in C++ || How to write Hello World in C++ || Writing First Program in C++
- How to Add Two Numbers in C++
- What are Control Structures in C++ || Understanding Control Structures in C++
- What are Functions and Recursion in C++ || How to Define and Call Functions
- Function Parameters and Return Types in C++ || Function Parameters || Function Return Types
- Function Overloading in C++ || What is Function Overloading
- Concept of OOP || What is OOP || Object-Oriented Programming Language
- Class in C++ || What is Class || What is Object || How to use Class and Object
- Object in C++ || How to Define Object in C++
- Polymorphism in C++ || What is Polymorphism || Types of Polymorphism
- Compile Time Polymorphism in C++
- Operator Overloading in C++ || What is Operator Overloading
- Python vs C++ || Difference Between Python and C++ || C++ vs Python
🐍 Python - Why Python is Best for Data
- Dynamic Programming in Python
- Difference Between Python and C
- Mojo vs Python – Key Differences
- Sentiment Analysis in Python
🌐 Web Development
🚀 Tech to Know & Technology
- The History and Evolution of Data Science
- The Importance of Data in Science
- Why Need Data Science?
- Scope of Data Science
- How to Present Yourself as a Data Scientist?
- Why Do We Use Tools Like Power BI and Tableau
- Data Exploration: A Simple Guide to Understanding Your Data
- What Is the Process of Collecting Import Data?
- Understanding Data Types
- Overview of Data Science Tools and Techniques
- Statistical Concepts in Data Science
- Descriptive Statistics in Data Science
- Data Visualization Techniques in Data Science
- Data Cleaning and Preprocessing in Data Science



