|
| 1 | +#include <iostream> |
| 2 | +#include <algorithm> // For std::sort |
| 3 | + |
| 4 | +/** |
| 5 | + * @brief Prints the elements of an integer array (vector). |
| 6 | + * |
| 7 | + * This function takes an array and its size as parameters, and prints all the |
| 8 | + * elements of the array, separating them with a dash (" - "). |
| 9 | + * |
| 10 | + * @param array The integer array to be printed. |
| 11 | + * @param size The size of the array. |
| 12 | + */ |
| 13 | + |
| 14 | +void printArray(const int *array, int size); |
| 15 | + |
| 16 | +/** |
| 17 | + * @brief Performs a binary search on a sorted array. |
| 18 | + * |
| 19 | + * This function implements the binary search algorithm, which efficiently finds |
| 20 | + * an element in a sorted array by repeatedly dividing the search interval in half. |
| 21 | + * |
| 22 | + * @param array The sorted array to search. |
| 23 | + * @param size The size of the array. |
| 24 | + * @param value The value to search for in the array. |
| 25 | + * @return The index of the element if found, or -1 if the element is not found. |
| 26 | + */ |
| 27 | + |
| 28 | +int binarySearch(const int *array, int size, int value); |
| 29 | + |
| 30 | +int main() |
| 31 | +{ |
| 32 | + // Initialize an array with 10 integer values |
| 33 | + int array[10] = {1, 45, 65, 22, 76, 55, 88, 81, 99, 96}; |
| 34 | + int valueToFind; |
| 35 | + |
| 36 | + // Sort the array before performing binary search |
| 37 | + std::sort(array, array + 10); |
| 38 | + |
| 39 | + // Print the sorted contents of the array |
| 40 | + printArray(array, 10); |
| 41 | + |
| 42 | + // Ask the user to enter a number they want to find |
| 43 | + std::cout << "Enter the number you want to find: "; |
| 44 | + std::cin >> valueToFind; |
| 45 | + |
| 46 | + // Use binary search to search for the value in the sorted array |
| 47 | + int index = binarySearch(array, 10, valueToFind); |
| 48 | + |
| 49 | + // Check if the value was found and display the result |
| 50 | + if (index != -1) |
| 51 | + { |
| 52 | + std::cout << "The value was found at position: " << index << std::endl; |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + std::cout << "The value was not found in the array.\n"; |
| 57 | + } |
| 58 | + |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +void printArray(const int *array, int size) |
| 63 | +{ |
| 64 | + std::cout << "Array: "; |
| 65 | + for (int i = 0; i < size; i++) |
| 66 | + { |
| 67 | + std::cout << array[i] << " - "; // Print each element followed by " - " |
| 68 | + } |
| 69 | + std::cout << std::endl; |
| 70 | +} |
| 71 | + |
| 72 | +int binarySearch(const int *array, int size, int value) |
| 73 | +{ |
| 74 | + int left{0}; |
| 75 | + int right{size - 1}; |
| 76 | + |
| 77 | + while (left <= right) |
| 78 | + { |
| 79 | + int mid = left + (right - left) / 2; // Calculate middle index |
| 80 | + |
| 81 | + // Check if the value is at the middle |
| 82 | + if (array[mid] == value) |
| 83 | + { |
| 84 | + return mid; // Value found, return its index |
| 85 | + } |
| 86 | + |
| 87 | + // If the value is greater, ignore the left half |
| 88 | + if (array[mid] < value) |
| 89 | + { |
| 90 | + left = mid + 1; |
| 91 | + } |
| 92 | + // If the value is smaller, ignore the right half |
| 93 | + else |
| 94 | + { |
| 95 | + right = mid - 1; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return -1; // Value not found |
| 100 | +} |
0 commit comments