I have a question regarding the implementation of an algorithm using Divide and conquer.
So, we have an unsorted array V[] and we have to find the k element of the array as if the array is sorted but without completely sort the array v.
Example:
Size of array = 8; k = 3; For example, if k = 3 and v = {2, 7, 5, 9, 8, 10, 3, 4} the k element of that array is 5.
Obs: The array is indexed from 0 to size - 1.
My code:
void read_STL(std::vector<int>& vect,int &k)
{
std::ifstream fisier("file.txt");
int dim;
fisier >> dim;
fisier >> k;
for (int i = 0; i < dim; i++)
{
int elem;
fisier >> elem;
vect.push_back(elem);
}
fisier.close();
}
bool criteriu(int i, int j)
{
return (i < j);
}
int search_DQ(std::vector<int>& vect, int st, int dr,int k)
{
if (st == dr)
{
return vect[st];
}
int mijl = (st + dr) / 2;
if (k == mijl)
{
return vect[k];
}
else if (k < mijl)
{
return search_DI(vect, st, mijl - 1, k);
}
else
{
return search_DQ(vect, mijl + 1, dr, k);
}
}
int main()
{
std::vector<int>vect;
int st, dr, k;
read_STL(vect,k);
st = 0; dr = vect.size() - 1;
std::cout<<search_DQ(vect, st, dr,k);
return 0;
}