JUST LAUNCHED
ATS Resume Checker NEW Get past the bots — see your matched roles and an ATS-ready resume. AI Mock Interview NEW Practice FAANG-style rounds with an AI that asks follow-ups.MOST USED
Resume Analyser Get your resume FAANG-ready — only the top 2% make the shortlist. Salary Analyser Check your FAANG-level pay instantly and see your salary gap. AI Quotient Is your resume AI-ready? Score it in under a minute.When it comes to coding interview prep for software developers or engineers, sorting algorithms is a topic you cannot afford to miss. Problems based on sorting algorithms regularly feature in tech interviews at FAANG and other tier-1 tech companies. In this article, we’ll help you review the iterative merge sort. Here’s what we will cover:
In Iterative merge sort, we implement merge sort in a bottom-up manner. This is how it works:
Let’s assume that the array Arr[] = {3, 2, 1, 9, 5, 4, 10, 11} of size N = 8 is to be sorted.
Arrays of length 1 are trivially sorted. First, we take sub_size = 1 and merge all pairs of sub-arrays of size 1.
Then, we multiply sub_size by 2, and sub_size becomes 2. Now, we merge all pairs of sub-arrays of size 2.
Again, we multiply sub_size by 2, and sub_size becomes 4, and we merge all pairs of sub-arrays of size 4.
Now, we stop, as sub_size is >= N and the array is sorted.
Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills
Consider an array Arr[] of size N that we want to sort:
Step 1: Initialize sub_size with 1 and multiply it by 2 as long as it is less than N. And for each sub_size, do the following:
Step 2: Initialize L with 0 and add 2*sub_size as long as it is less than N. Calculate Mid as min(L + sub_size – 1, N-1) and R as min(L + (2* sub_size) -1, N-1) and do the following:
Step 3: Copy sub-array [L, Mid-1] in list A and sub-array [Mid, R] in list B and merge these sorted lists to make a sorted list C using the following method:
Step 3.1: Compare the first elements of lists A and B and remove the first element from the list whose first element is smaller and append it to C. Repeat this until either list A or B becomes empty.
Step 3.2: Copy the list(A or B), which is not empty, to C.
Step 4: Copy list C to Arr[] from index L to R.
Recursive Merge Sort Implementation
Here’s the implementation of recursive merge sort algorithm in C++:
#include<bits/stdc++.h>
using namespace std;
void merge(int Arr[], int l, int m, int r) {
int i, j, k;
int n1 = m – l + 1;
int n2 = r – m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = Arr[l + i];
for (j = 0; j < n2; j++)
R[j] = Arr[m + 1 + j];
i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
Arr[k] = L[i];
i++;
} else {
Arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
Arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
Arr[k] = R[j];
j++;
k++;
}
}
void merge_sort(int L, int R, int Arr[]){
if(L==R)
return ;
int Mid= (L+R)/2;
// Dividing sub-array from L to R into
// two parts and recursively solving
merge_sort(L, Mid, Arr);
merge_sort(Mid+1, R, Arr);
// merging two sorted sub-arrays
merge(Arr,L, Mid, R);
}
int main()
{
int i;
int N = 8;
int Arr[N] = {3, 2, 1, 9, 5, 4, 10, 11};
cout<<“Unsorted Array: “;
for(i=0;i<N;i++)
cout<<Arr[i]<<” “;
cout<<endl;
merge_sort(0, N-1, Arr);
cout<<“Sorted Array: “;
for(i=0;i<N;i++)
cout<<Arr[i]<<” “;
return 0;
}
Unsorted Array: 3 2 1 9 5 4 10 11
Sorted Array: 1 2 3 4 5 9 10 11
And this is how iterative merge sort can be implemented in C++:
#include<bits/stdc++.h>
using namespace std;
void merge(int Arr[], int l, int m, int r) {
int i, j, k;
int n1 = m – l + 1;
int n2 = r – m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = Arr[l + i];
for (j = 0; j < n2; j++)
R[j] = Arr[m + 1+ j];
i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
Arr[k] = L[i];
i++;
} else {
Arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
Arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
Arr[k] = R[j];
j++;
k++;
}
}
void merge_sort(int Arr[], int N){
for(int sub_size=1;sub_size<N;sub_size*=2)
{
for(int L=0; L<N; L+=(2*sub_size))
{
int Mid=min(L+sub_size-1,N-1);
int R=min(L+2*sub_size-1,N-1);
// function to merge two sub-arrays of
// size sub_size starting from L and Mid
merge(Arr, L,Mid,R);
}
}
}
int main()
{
int i;
int N = 8;
int Arr[N] = {3, 2, 1, 9, 5, 4, 10, 11};
cout<<“Unsorted Array: “;
for(i=0;i<N;i++)
cout<<Arr[i]<<” “;
cout<<endl;
merge_sort(Arr, N);
cout<<“Sorted Array: “;
for(i=0;i<N;i++)
cout<<Arr[i]<<” “;
return 0;
}
Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills
Unsorted Array: 3 2 1 9 5 4 10 11
Sorted Array: 1 2 3 4 5 9 10 11
Given: N = 8, Arr[] = {3, 2, 1, 9, 5, 4, 10, 11}
Arr[] = {2, 3, 1, 9, 4, 5, 10, 11}
Arr[] = {1, 2, 3, 9, 4, 5, 10, 11}
Arr[] = {1, 2, 3, 4, 5, 9, 10, 11}
To know more about the merge sort vs. quicksort, read:
Difference Between Merge Sort and Quicksort
Merge Sort vs. Quicksort: Algorithm Performance Analysis
Question 1: Is iterative merge sort stable?
Answer: Yes, iterative merge sort is an example of a stable sorting algorithm, as it does not change the relative order of elements of the same value in the input.
Question 2. Is iterative merge sort an in-place sorting algorithm?
Answer: No, iterative merge sort is not an in-place sorting algorithm. In in-place sorting algorithms, only a small/constant auxiliary space is used; in iterative merge sort, we use auxiliary lists to merge to sub-arrays.
If you’re looking for guidance and help with getting started, sign up for our free webinar. As pioneers in the field of technical interview prep, we have trained thousands of software engineers to crack the toughest coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!
———-
Article contributed Abhinav Tiwari
FREE IK TOOLS
See how your resume scores and what to fix before you apply.
Analyse my resume →Takes ~30 seconds · no spam
Benchmark your pay against FAANG+ offers and see where you stand.
Analyse my salary →Takes ~60 seconds · no spam
Discover your AI-Readiness Score and what to learn next.
Get my AI score →Takes ~30 seconds · no spam
Free Tools · No Credit Card Needed
Three free analysers, benchmarked against real FAANG+ hiring data.
See your resume the way an ATS and a FAANG+ recruiter do - parse score, keyword gaps, seniority signals.
Score my resumeKnow your true market value, where you rank against peers, and the hike AI skills unlock.
Check my bandYour AI-Readiness Score, the skill gaps behind it, and a personalised roadmap to close them.
Get my AIQ scoreMaster ML interviews with DSA, ML System Design, Supervised/Unsupervised Learning, DL, and FAANG-level interview prep.
Get strategies to ace TPM interviews with training in program planning, execution, reporting, and behavioral frameworks.
Course covering SQL, ETL pipelines, data modeling, scalable systems, and FAANG interview prep to land top DE roles.
Course covering Embedded C, microcontrollers, system design, and debugging to crack FAANG-level Embedded SWE interviews.
Nail FAANG+ Engineering Management interviews with focused training for leadership, Scalable System Design, and coding.
End-to-end prep program to master FAANG-level SQL, statistics, ML, A/B testing, DL, and FAANG-level DS interviews.
Learn to build AI agents to automate your repetitive workflows
Upskill yourself with AI and Machine learning skills
Prepare for the toughest interviews with FAANG+ mentorship
Time Zone:
Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills
25,000+ Professionals Trained
₹23 LPA Average Hike 60% Average Hike
600+ MAANG+ Instructors
Webinar Slot Blocked
Register for our webinar
Learn about hiring processes, interview strategies. Find the best course for you.
ⓘ Used to send reminder for webinar
Time Zone: Asia/Kolkata
Time Zone: Asia/Kolkata
Hands-on AI/ML learning + interview prep to help you win
Explore your personalized path to AI/ML/Gen AI success
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills
Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills
Webinar Slot Blocked
Time Zone: Asia/Kolkata
Hands-on AI/ML learning + interview prep to help you win
Time Zone: Asia/Kolkata
Hands-on AI/ML learning + interview prep to help you win
Explore your personalized path to AI/ML/Gen AI success
See you there!