Sorting Algorithms - Study Mode
[#491] What will be the output of the given C++ code? #include <bits/stdc++.h>
using namespace std
int main()
{
int arr[] = {1, 3,4,2,5}
int n = sizeof(arr)/sizeof(arr[0])
sort(arr, arr+n)
int a
for ( a = 0
a< n
a++)
cout << arr[a] << " "
return 0
}
Correct Answer
(A) 1 2 3 4 5
[#492] 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++
}
}
}
Correct Answer
(B) O(n 2 )
[#493] What will be the base case for the code of recursive insertion sort ?
Correct Answer
(C) if(n <= 1)
return
[#494] What will be the output of the given C++ code? #include <bits/stdc++.h>
using namespace std
int main()
{
int arr[] = {1, 3,4,2,5}
int n = sizeof(arr)/sizeof(arr[0])
sort(arr+2, arr+n, greater<int>())
int a
for (int a = 0
a < n
a++)
cout << arr[a] << " "
return 0
}
Correct Answer
(D) 1 3 5 4 2
[#495] There is a one line error in the following routine. Find that line. 1. int Max(int a[], int n)
2. {
3. int mi, i
4. for (mi = 0, i = 0
i < n
i++)
5. if (a[i] > a[mi])
6. mi = i
7. return mi
8. }
Correct Answer
(B) Line 4