Is there any difference in speed between first loop and a second one when it comes to speed of executing it? Im talking also about larger arrays than this sample one.
int main()
{
int n = 17;
int posz = 7;
int array[] = {0, 1, 9, 2, 8, 3, 8, 5, 4, 7, 6, 10, 23, 43, 123, 54, 76, 34};
for (int i = 0; i < (n / 2) + 1; i++)
{
if (array[i] == posz || array[n - 1 - i] == posz)
{
std::cout << "found\n";
break;
}
}
for (int i = 0; i < n; i++)
{
if (array[i] == posz)
{
std::cout << "found\n";
break;
}
}
return 0;
}
std::find
algorithm function would figure out to write the loop that way. – PaulMcKenzie Nov 2 '20 at 2:00