Advertisement
Stoycho_KK

рекурсивно търсене на най голям елемент чрез разделяне на масива на две части

Jan 20th, 2021 (edited)
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. int getMaxElement(int* elementsArr, int startingPoint, int endingPoint) {
  4.     int middleIndex = endingPoint - (endingPoint + startingPoint)/2;
  5.  
  6.         if (middleIndex == 0) {
  7.             if (elementsArr[startingPoint] > elementsArr[endingPoint])
  8.                 return elementsArr[startingPoint];
  9.             else
  10.                 return elementsArr[endingPoint];
  11.         }
  12.  
  13.         if (getMaxElement(elementsArr, startingPoint + middleIndex, endingPoint) > getMaxElement(elementsArr, startingPoint, endingPoint - middleIndex))
  14.             return getMaxElement(elementsArr, startingPoint + middleIndex, endingPoint);
  15.         else
  16.             getMaxElement(elementsArr, startingPoint, endingPoint - middleIndex);
  17.  
  18. }
  19.  
  20. int main() {
  21.     int arr[10] = { 1, 2, 12, 22, -1, 3, -2, 7, 0, 12 };
  22.     std::cout << getMaxElement(arr, 0, 9);
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement