CARVIEW |
Select Language
HTTP/2 200
content-type: text/html; charset=utf-8
date: Sun, 31 Aug 2025 03:46:23 GMT
permissions-policy: interest-cohort=()
strict-transport-security: max-age=31536000; includeSubDomains
server: nginx
cache-control: s-maxage=58710, max-age=0
x-powered-by: Next.js
etag: "mtwcrech869ogp"
content-encoding: gzip
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
referrer-policy: no-referrer-when-downgrade
content-security-policy: default-src 'self' http: https: ws: wss: data: blob: 'unsafe-eval' 'unsafe-inline'; frame-ancestors 'self';
vary: Accept-Encoding
x-cache: Miss from cloudfront
via: 1.1 8245690a788e53c4f00d1ddf4345d9ec.cloudfront.net (CloudFront)
x-amz-cf-pop: BOM78-P9
x-amz-cf-id: Y4SMcsz31LYOEV0uWhCA_pNajI_vZJLV8veZlWZzExM1YEs31WvZrQ==
Quiz on Complexity analysis for DSA: Question 9
C++
C
Java
Python
JavaScript
Interview Preparation
- Interview Preparation For Software Developers
- Must Coding Questions - Company-wise
- Must Do Coding Questions - Topic-wise
- Company-wise Practice Problems
- Company Preparation
- Competitive Programming
- Software Design-Patterns
- Company-wise Interview Experience
- Experienced - Interview Experiences
- Internship - Interview Experiences
Quiz on Complexity analysis for DSA: Question 9
Last Updated :
Discuss
Comments
What is the time complexity of the following recursive function?
Note : The merge function takes linear time.
vector<int> mergeSort(const vector<int>& arr) {
if (arr.size() <= 1) {
return arr;
}
int mid = arr.size() / 2;
vector<int> left = mergeSort(vector<int>(arr.begin(), arr.begin() + mid));
vector<int> right = mergeSort(vector<int>(arr.begin() + mid, arr.end()));
return merge(left, right);
}
void mergeSort(int arr[], int size) {
if (size <= 1) {
return;
}
int mid = size / 2;
int* left = (int*)malloc(mid * sizeof(int));
int* right = (int*)malloc((size - mid) * sizeof(int));
for (int i = 0; i < mid; i++) {
left[i] = arr[i];
}
for (int i = mid; i < size; i++) {
right[i - mid] = arr[i];
}
mergeSort(left, mid);
mergeSort(right, size - mid);
merge(arr, left, mid, right, size - mid);
}
public static int[] mergeSort(int[] arr) {
if (arr.length <= 1) {
return arr;
}
int mid = arr.length / 2;
int[] left = mergeSort(Arrays.copyOfRange(arr, 0, mid));
int[] right = mergeSort(Arrays.copyOfRange(arr, mid, arr.length));
return merge(left, right);
}
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
return merge(left, right)
function mergeSort(arr) {
if (arr.length <= 1) {
return arr;
}
const mid = Math.floor(arr.length / 2);
const left = mergeSort(arr.slice(0, mid));
const right = mergeSort(arr.slice(mid));
return merge(left, right);
}
O(log n)
O(n log n)
O(n^2)
O(n)
This question is part of this quiz :
Quiz on Complexity analysis for DSATags:
Share your thoughts in the comments

GeeksforGeeks
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy