Explain the functionality of the following functions.
Question 1
C
int fun1( int x, int y)
{
if (x == 0)
return y;
else
return fun1(x - 1, x + y);
}
|
C++
int fun1( int x, int y)
{
if (x == 0)
return y;
else
return fun1(x - 1, x + y);
}
|
Java
static int fun1( int x, int y)
{
if (x == 0 )
return y;
else
return fun1(x - 1 , x + y);
}
|
Python3
def fun1(x, y) :
if (x = = 0 ) :
return y
else :
return fun1(x - 1 , x + y)
|
C#
static int fun1( int x, int y)
{
if (x == 0)
return y;
else
return fun1(x - 1, x + y);
}
|
Answer: The function fun() calculates and returns ((1 + 2 … + x-1 + x) +y) which is x(x+1)/2 + y. For example if x is 5 and y is 2, then fun should return 15 + 2 = 17.
Question 2
C++
int minIndex( int arr[], int s, int e)
{
int sml = INT32_MAX;
int mindex;
for ( int i = s; i < e; i++) {
if (sml > arr[i]) {
sml = arr[i];
mindex = i;
}
}
return mindex;
}
void fun2( int arr[], int start_index, int end_index)
{
if (start_index >= end_index)
return ;
int min_index;
int temp;
min_index = minIndex(arr, start_index, end_index);
swap(arr[start_index], arr[min_index]);
fun2(arr, start_index + 1, end_index);
}
|
C
int minIndex( int arr[], int s, int e)
{
int sml = INT32_MAX;
int mindex;
for ( int i = s; i < e; i++) {
if (sml > arr[i]) {
sml = arr[i];
mindex = i;
}
}
return mindex;
}
void fun2( int arr[], int start_index, int end_index)
{
if (start_index >= end_index)
return ;
int min_index;
int temp;
min_index = minIndex(arr, start_index, end_index);
temp = arr[start_index];
arr[start_index] = arr[min_index];
arr[min_index] = temp;
fun2(arr, start_index + 1, end_index);
}
|
Java
static int minIndex( int arr[], int s, int e)
{
int sml = Integer.MAX_VALUE;
int mindex = ;
for ( int i = s; i < e; i++) {
if (sml > arr[i]) {
sml = arr[i];
mindex = i;
}
}
return mindex;
}
static void fun2( int arr[], int start_index, int end_index)
{
if (start_index >= end_index)
return ;
int min_index;
int temp;
min_index = minIndex(arr, start_index, end_index);
temp = arr[start_index];
arr[start_index] = arr[min_index];
arr[min_index] = temp;
fun2(arr, start_index + 1 , end_index);
}
|
Python3
def minIndex(arr, s, e):
sml = sys.maxsize
mindex = 0
for i in range (s, e):
if (sml > arr[i]):
sml = arr[i]
mindex = i
return mindex
def fun2(arr, start_index, end_index):
if (start_index > = end_index):
return
min_index = minIndex(arr, start_index, end_index)
arr[start_index], arr[min_index] = arr[min_index], arr[start_index]
fun2(arr, start_index + 1 , end_index)
|
C#
static int minIndex( int [] arr, int s, int e)
{
int sml = Int32.MaxValue;
int mindex;
for ( int i = s; i < e; i++)
{
if (sml > arr[i])
{
sml = arr[i];
mindex = i;
}
}
return mindex;
}
static void fun2( int [] arr, int start_index, int end_index)
{
if (start_index >= end_index)
{
return ;
}
int min_index;
int temp;
min_index = minIndex(arr, start_index, end_index);
temp = arr[start_index];
arr[start_index] = arr[min_index];
arr[min_index] = temp;
fun2(arr, start_index + 1, end_index);
}
|
Answer: The function fun2() is a recursive implementation of Selection Sort.
Please write comments if you find any of the answers/codes incorrect, or you want to share more information about the topics discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.