O(n log n) – Linearithmic Time Complexity
After learning O(1), O(log n), and O(n), it's time to understand another very important time complexity: O(n log n).
At first glance, O(n log n) may look confusing because it combines two mathematical terms: n and log n. But don't worry! Once you understand the idea behind it, you'll realize why many of the fastest sorting algorithms use this complexity.
In fact, O(n log n) is considered one of the most efficient time complexities for solving large-scale problems.
{tocify} $title={Table of Contents}
What is O(n log n)?
O(n log n) means that an algorithm performs log n operations for each of the n elements.
In simple words:
- The algorithm goes through all the elements.
- For each element, it performs a logarithmic amount of work.
Because of this combination, the total work becomes n × log n, which we write as O(n log n).
Although this is slower than O(n), it is still much faster than O(n²) when working with large amounts of data.
Simple Definition
Linearithmic Time Complexity (O(n log n)) means the running time grows slightly faster than linear time because the algorithm processes every element while repeatedly dividing the problem into smaller parts.
Real-Life Example
Imagine you are a teacher checking exam papers for 1,000 students.
Instead of arranging all the papers manually from start to finish, you first divide them into smaller groups.
Then you continue dividing each group until they become easy to organize.
Finally, you combine all the sorted groups together.
This method is much faster than comparing every paper with every other paper.
This is exactly how algorithms like Merge Sort work.
Why is O(n log n) Important?
Many real-world applications need to sort or organize huge amounts of data.
For example:
- Google sorts billions of search results.
- Amazon sorts products based on ratings and prices.
- Netflix organizes movie recommendations.
- Banking systems sort millions of transactions every day.
If these systems used slower algorithms like O(n²), they would become much slower as the amount of data increased.
That is why most modern sorting algorithms are designed to achieve O(n log n) performance.
Algorithms That Use O(n log n)
Some popular algorithms with O(n log n) time complexity are:
- Merge Sort
- Heap Sort
- Quick Sort (Average Case)
- TimSort (used in Python)
- IntroSort (used in C++ STL
sort())
These algorithms are preferred because they can efficiently handle very large datasets.
Understanding O(n log n) with Numbers
Suppose we have different input sizes.
| Number of Elements (n) | log₂(n) | Total Operations (n × log₂(n)) |
|---|---|---|
| 8 | 3 | 24 |
| 16 | 4 | 64 |
| 32 | 5 | 160 |
| 64 | 6 | 384 |
| 128 | 7 | 896 |
| 1,024 | 10 | 10,240 |
Notice something interesting.
When the input doubles, the work does not double dramatically like O(n²).
Instead, it increases gradually, making O(n log n) a practical choice for large applications.
Visual Comparison
Imagine you have 1,000 books that need to be arranged alphabetically.
Method 1
Compare every book with every other book.
This takes a very long time.
Method 2
Divide the books into smaller groups.
Sort each group.
Merge the groups together.
This finishes much faster.
That second method represents O(n log n).
C++ Example Using Merge Sort
#include <iostream>
using namespace std;
void merge(int arr[], int left, int mid, int right)
{
int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];
for(int i = 0; i < n1; i++)
L[i] = arr[left + i];
for(int j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while(i < n1 && j < n2)
{
if(L[i] <= R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}
while(i < n1)
arr[k++] = L[i++];
while(j < n2)
arr[k++] = R[j++];
}
void mergeSort(int arr[], int left, int right)
{
if(left < right)
{
int mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
int main()
{
int arr[] = {40, 20, 70, 10, 90, 50};
int size = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, size - 1);
cout << "Sorted Array:\n";
for(int i = 0; i < size; i++)
cout << arr[i] << " ";
return 0;
}
Output
Sorted Array:
10 20 40 50 70 90
Why Does Merge Sort Have O(n log n)?
Merge Sort follows two important steps.
Step 1: Divide
The array is repeatedly divided into two halves.
For example:
40 20 70 10 90 50
↓
40 20 70 | 10 90 50
↓
40 | 20 70 | 10 | 90 50
↓
40 | 20 | 70 | 10 | 90 | 50
Each division cuts the problem into half.
This contributes the log n part.
Step 2: Merge
After dividing, Merge Sort combines the smaller sorted arrays.
During merging, every element is visited once.
This contributes the n part.
Therefore,
Time Complexity = O(n × log n)
= O(n log n)
Another Simple Example
Suppose you have 16 students.
You divide them into two groups.
16
↓
8 + 8
↓
4 + 4 + 4 + 4
↓
2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
↓
1 + 1 + 1 + 1 + ...
The number of divisions is only 4, because:
log₂16 = 4
Now imagine checking every student during every level.
That becomes:
16 × 4
= 64 operations
Which is much smaller than:
16² = 256 operations
O(n log n) vs O(n²)
| Input Size | O(n log n) | O(n²) |
|---|---|---|
| 100 | 664 | 10,000 |
| 1,000 | 9,966 | 1,000,000 |
| 10,000 | 132,877 | 100,000,000 |
As you can see, the difference becomes huge as the input size grows.
This is why software engineers always prefer O(n log n) algorithms whenever possible.
Where Is O(n log n) Used?
You may not realize it, but you use O(n log n) algorithms almost every day.
Some examples include:
- Sorting files on your computer
- Organizing photos by date
- Ranking search results on Google
- Sorting products on shopping websites
- Arranging playlists in music apps
- Banking transaction processing
- Data analytics and reporting
- Database indexing
- Machine Learning data preprocessing
Advantages of O(n log n)
- Much faster than O(n²) for large datasets.
- Handles millions of records efficiently.
- Used in many real-world software applications.
- Perfect for sorting and divide-and-conquer algorithms.
- Commonly asked in coding interviews.
Disadvantages of O(n log n)
- Slightly slower than O(n).
- Can be more difficult to implement than simple algorithms.
- Some algorithms require additional memory (for example, Merge Sort).
Quick Revision
- O(n log n) combines linear and logarithmic growth.
- It is commonly used in efficient sorting algorithms.
- Merge Sort, Heap Sort, and the average case of Quick Sort have O(n log n) time complexity.
- It performs much better than O(n²) for large inputs.
- Many modern applications rely on O(n log n) algorithms to process large amounts of data quickly.
🧩 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



