Here's my implementation using divide and conquer in C++.
What do you think of this implementation regarding running time ?
#include <iostream>
#include <tuple>
using namespace std;
tuple <int,int> largest_two_numbers(int *arr,int size)
{
//The algorithm is based on returning a tuple with the largest two numbers in the array
if (size==1)
return tuple<int,int>(arr[0],arr[0]);
if (size==2)
return tuple<int,int>(arr[0],arr[1]);
//Split the arr into two arrays
int arr_1[size/2],arr_2[size/2+(size&1)];
for (int i = 0; i < size/2; ++i)
{
arr_1[i]=arr[i];
arr_2[i]=arr[i+size/2];
}
if (size&1) arr_2[size/2]=arr[size-1];
//Return two tuples one for the right half and the other for the left
tuple <int,int> right= largest_two_numbers(arr_1,size/2);
tuple <int,int> left = largest_two_numbers(arr_2,size/2+(size&1));
//Declare an array maxis with the values inside the two tuples combined and find the largest two numbers
int maxis[]={get<0>(right),get<1>(right),get<0>(left),get<1>(left)};
int max1=maxis[0],max2=maxis[0];
for (int i = 0; i < 4; ++i)
{
if (maxis[i]>max1) max1=maxis[i];
}
for (int i = 0; i < 4; ++i)
{
if (maxis[i]>max2 && maxis[i]<max1) max2=maxis[i];
}
//Return a tuple with the largest two numbers
return tuple<int,int>(max1,max2);
}
int main(){
int arr[]={-1,98,22,1,0,301,112},n=7;
//Print the second largest element in the array which is the second element in the tupple
cout << get<1>(largest_two_numbers(arr,n));
}