added binary search

This commit is contained in:
AbhineshJha 2023-10-08 19:56:01 +05:30
parent d0890798d7
commit 9636bd7df6

View File

@ -15,29 +15,28 @@
* \returns index of a peak element in the array * \returns index of a peak element in the array
* \returns -1 if no peak element is found * \returns -1 if no peak element is found
*/ */
int FindPeakElement(int *array, int size) { #include <iostream>
#include <cassert>
int findPeakElement(int *array, int left, int right, int size) {
if (size == 0) { if (size == 0) {
return -1; // No elements in the array return -1; // No elements in the array
} }
if (size == 1) { while (left <= right) {
return 0; // Single element is a peak int mid = left + (right - left) / 2;
}
// Check the first element // Check if mid is a peak element
if (array[0] >= array[1]) { if ((mid == 0 || array[mid] >= array[mid - 1]) && (mid == size - 1 || array[mid] >= array[mid + 1])) {
return 0; return mid;
} }
// If the element to the right of mid is greater, move right
// Check the last element else if (mid < size - 1 && array[mid] < array[mid + 1]) {
if (array[size - 1] >= array[size - 2]) { left = mid + 1;
return size - 1; }
} // Otherwise, move left
else {
// Check elements in the middle right = mid - 1;
for (int i = 1; i < size - 1; ++i) {
if (array[i] >= array[i - 1] && array[i] >= array[i + 1]) {
return i;
} }
} }
@ -52,23 +51,24 @@ int FindPeakElement(int *array, int size) {
static void tests() { static void tests() {
int size = 6; int size = 6;
int array[] = {1, 3, 20, 4, 1, 0}; int array[] = {1, 3, 20, 4, 1, 0};
assert(FindPeakElement(array, size) == 2); assert(findPeakElement(array, 0, size - 1, size) == 2);
size = 5; size = 5;
int array2[] = {5, 10, 20, 15, 5}; int array2[] = {5, 10, 20, 15, 5};
assert(FindPeakElement(array2, size) == 2); assert(findPeakElement(array2, 0, size - 1, size) == 2);
size = 4; size = 4;
int array3[] = {10, 20, 15, 2}; int array3[] = {10, 20, 15, 2};
assert(FindPeakElement(array3, size) == 1); assert(findPeakElement(array3, 0, size - 1, size) == 1);
size = 1; size = 1;
int array4[] = {5}; int array4[] = {5};
assert(FindPeakElement(array4, size) == 0); assert(findPeakElement(array4, 0, size - 1, size) == 0);
std::cout << "All tests have successfully passed!" << std::endl; std::cout << "All tests have successfully passed!" << std::endl;
} }
/** /**
* @brief Main function * @brief Main function
* @returns 0 on exit * @returns 0 on exit
@ -92,15 +92,14 @@ int main() {
} }
int *array = new int[size]; int *array = new int[size];
int key = 0;
// Input for the array elements // Input for the array elements
std::cout << "Enter the array of " << size << " numbers: "; std::cout << "Enter " << size << " numbers in ascending order: ";
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
std::cin >> array[i]; std::cin >> array[i];
} }
int peakIndex = FindPeakElement(array, size); int peakIndex = findPeakElement(array, 0, size - 1, size);
if (peakIndex != -1) { if (peakIndex != -1) {
std::cout << "Peak element is " << array[peakIndex] << " at index: " << peakIndex << std::endl; std::cout << "Peak element is " << array[peakIndex] << " at index: " << peakIndex << std::endl;
} else { } else {
@ -113,3 +112,4 @@ int main() {
} }
return 0; return 0;
} }