C++ Array Searching šŸŽÆ

beginner
7 min

C++ Array Searching šŸŽÆ

Welcome to the exciting world of C++! Today, we'll dive into searching within arrays – a fundamental concept that you'll encounter frequently in your coding journey. Let's get started!

Arrays šŸ“

Before we delve into array searching, let's briefly understand what arrays are. In C++, arrays are used to store multiple values of the same data type in a contiguous block of memory.

Here's a simple example of creating an array:

cpp
int numbers[5] = {1, 2, 3, 4, 5};

In this example, we have an array numbers that stores integer values.

Linear Search šŸ’”

Linear search is the simplest way to find a specific value within an array. The algorithm checks each element one by one, starting from the first element, until it finds the desired value or reaches the end of the array.

Here's a code example for linear search:

cpp
#include <iostream> using namespace std; int linearSearch(int arr[], int size, int target) { for (int i = 0; i < size; i++) { if (arr[i] == target) { return i; // Return the index if found } } return -1; // Return -1 if not found } int main() { int numbers[5] = {1, 2, 3, 4, 5}; int target = 3; int result = linearSearch(numbers, 5, target); if (result != -1) { cout << "Element found at index: " << result << endl; } else { cout << "Element not found." << endl; } return 0; }

In this example, we've created a function linearSearch to perform the linear search operation. The main function demonstrates how to use this function to find a specific value in an array.

Binary Search šŸ’”

Binary search is a more efficient method for searching arrays that are sorted in ascending order. Instead of checking each element one by one, it divides the array into two halves and compares the target value with the middle element. Based on the comparison result, it recursively searches the appropriate half.

Here's a code example for binary search:

cpp
#include <iostream> using namespace std; int binarySearch(int arr[], int size, int target, int left, int right) { if (left <= right) { int mid = (left + right) / 2; if (arr[mid] == target) { return mid; // Return the index if found } if (arr[mid] < target) { return binarySearch(arr, size, target, mid + 1, right); } else { return binarySearch(arr, size, target, left, mid - 1); } } return -1; // Return -1 if not found } int main() { int numbers[5] = {1, 2, 3, 4, 5}; int target = 3; int result = binarySearch(numbers, 5, target, 0, 4); if (result != -1) { cout << "Element found at index: " << result << endl; } else { cout << "Element not found." << endl; } return 0; }

In this example, we've created a function binarySearch to perform the binary search operation. The main function demonstrates how to use this function to find a specific value in a sorted array.

Quiz šŸ“

Quick Quiz
Question 1 of 1

What is the primary difference between linear search and binary search?

Now that you've learned about linear and binary searches, you're ready to practice searching arrays in C++! Keep practicing and improving your skills. Happy coding! šŸš€šŸ’»