Sorting Algorithms - Study Mode
[#496] There is one small error in the following flip routine. Find out which line it is on. 1 void flip(int arr[], int i)
2 {
3 int t, init = 0
4 while (init < i)
5 {
6 t = arr[init]
7 arr[i] = arr[init]
8 arr[i] = t
9 init++
10 i--
11 }
12 }
Correct Answer
(C) Line 7
[#497] 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, greater<int>())
int a
for (a = 0
a < n
a++)
cout << arr[a] << " "
return 0
}
Correct Answer
(C) 5 4 3 2 1
[#498] Which of the following statements is the basic for loop for a shell sort algorithm?
Correct Answer
(A) for(increment=N/2
increment>0
increment/=2)
[#499] Choose the correct option to fill? X so that the code given below implements the Heap sort. #include <stdio.h>
void heapify(int arr[], int n, int i)
{
int largest = i
// Initialize largest as root
int l = 2*i + 1
// left = 2*i + 1
int r = 2*i + 2
// right = 2*i + 2
if (l < n && arr[l] > arr[largest])
largest = l
if (r < n && arr[r] > arr[largest])
largest = r
if (largest != i)
{
swap(arr[i], arr[largest])
heapify(arr, n, largest)
}
}
void heapSort(int arr[], int n)
{
for (int i = n / 2 - 1
i >= 0
i--)
heapify(arr, n, i)
for (int i=n-1
i>=0
i--)
{
X
heapify(arr, i, 0)
}
}
void printArray(int arr[], int n)
{
for (int i=0
i<n
++i)
printf(“%d”,arr[i])
printf(“
”)
}
int main()
{
int arr[] = {12, 11, 13, 5, 6, 7}
int n = sizeof(arr)/sizeof(arr[0])
heapSort(arr, n)
printf(“Sorted array is
")
printArray(arr, n)
}
Correct Answer
(C) swap(arr[0], arr[i])
[#500] Which of the following function chooses a random index as pivot.
Correct Answer
(A) void partition_random(int arr[], int low, int high)
{
srand(time(NULL))
int random = low + rand() % (high - low)
swap(arr[random], arr[high])
}