What is the average time complexity of in place merge sort when we use the following function for merging?
void in_place_merge(int arr[], int l, int middle, int r)
{
int start2 = middle + 1;
if (arr[middle] <= arr[start2])
{
return;
}
while (l <= middle && start2 <= r)
{
if (arr[l] <= arr[start2])
{
l++;
}
else
{
int val = arr[start2];
int index = start2;
while (index != l)
{
arr[index] = arr[index - 1];
index--;
}
arr[l] = val;
l++;
middle++;
start2++;
}
}
}
void in_place_merge(int arr[], int l, int middle, int r)
{
int start2 = middle + 1;
if (arr[middle] <= arr[start2])
{
return;
}
while (l <= middle && start2 <= r)
{
if (arr[l] <= arr[start2])
{
l++;
}
else
{
int val = arr[start2];
int index = start2;
while (index != l)
{
arr[index] = arr[index - 1];
index--;
}
arr[l] = val;
l++;
middle++;
start2++;
}
}
}
A. O(n log n)
B. O(n2)
C. O(n2 log n)
D. O(n log n2)
Answer: Option B
Related Questions on Sorting Algorithms
What is the time complexity of Bubble Sort in the average case?
A. O(n log n)
B. O(n)
C. O(n2)
D. O(n3)
Which sorting algorithm is based on the divide-and-conquer strategy?
A. Insertion Sort
B. Quick Sort
C. Selection Sort
D. Insertion Sort
Which sorting algorithm uses a "pivot" element to partition the array into sub-arrays?
A. Merge Sort
B. Heap Sort
C. Counting Sort
D. Quick Sort
Join The Discussion