Contents Menu Expand Light mode Dark mode Auto light/dark, in light mode Auto light/dark, in dark mode Skip to content
🚀 Welcome to Mitchell Systems, A Documentation Portfolio
Logo

Contents:

  • About Me
  • Resume
  • Topics
    • Web Development
    • Data Structures
      • Dynamic Data Structures
        • 2- Binary Search
        • 3- Dynamic Data Structures
        • 4- Stacks and Queues
        • 5-Binary Search Trees (BSTs)
        • 6-Tries and Adapting Data Structures
        • 7-Priority Queues and Heaps
        • 8- GRIDS
        • 9- Spatial Trees
        • 10- Hash Tables
        • 11- Caches
        • 12- B-Trees
        • 13- Bloom Filters
        • 14- Skip Lists
        • 15- Graphs
      • Memory Project
        • Memory Project Code
      • Skip Lists Final Project
    • Software Engineering
      • Containers
      • Using Vim
      • Gannt
        • Useful Features of GanttProject
      • Git Project
      • Python Testing
        • Python Testing Notes
        • Python Testing Quicklist(GPT)
        • Python Testing With a Queue
        • Calc and Converter Tests
      • Coding Styles
      • Software Development Lifecycle
      • Taiga + Scrum Usage Guide
      • Refactoring in C++
      • Proactive Security in Software Engineering
    • Operating Systems
      • Operating Systems Theory
        • Chapter 1: Introduction to Operating Systems
        • Chapter 2: Operating System Structures
        • Chapter 3: Processes
        • Chapter 4: Threads
        • Chapter 5: CPU Scheduling
        • Chapter 6: Process Synchronization
        • Chapter 7: Deadlocks
        • Chapter 8: Main Memory
        • Chapter 9: Virtual Memory
        • Chapter 10: Mass-Storage Structure
        • Chapter 11: File-System Interface
        • Chapter 12: File-System Implementation
        • Chapter 13: I/O Systems
        • Chapter 14: System Protection and Security
      • Building the Server Manual
      • Configuration Management Recommendation Letter
      • Recommendation for a Server Backup System
    • Cyber Analysis and Reporting
      • Historical Cyber Attacks
      • Vulnerability Assessment
      • My Systems Security Assessment
      • Capstone Project: Incident Investigation
    • Programming Languages
      • Job Interview Lecture Notes
      • Paradigm Tree
        • Paradigm Tree Project Plan
        • Data
        • AI Conversation Doc
        • Programming Language Tree Graphic and Code
      • Prolog
        • Prolog Tutorial
        • All Prolog Programs
      • Programmer’s Python: Everything Is An Object
        • 1: Get Ready For The Python Difference
        • 2: Variables, Objects and Attributes
        • 3: The Function Object
        • 4: Scope, Lifetime, and Closure
        • 5: Advanced Functions
        • 6: Decorators
        • 7: Class, Methods and Constructors
        • 8: Inside Class
        • 9: Meeting Metaclasses
        • 10: Advanced Attributes
        • 11: Custom Attribute Access
        • 12: Single Inheritance
        • 13: Multiple Inheritance
        • 14: Class and Type
        • 15: Type Annotation
        • 16: Operator Overloading
      • Python Documentation
        • Python Description
        • Halting in Python
        • Formalisms for Computability
        • Names and Environment in Python
        • Memory Management in Python
        • Control Structures in Python
        • Control Abstraction in Python
        • Structuring Data In Python
        • Data Abstraction in Python
    • Network and System Security
      • Labs
        • Designing a Secure Network Topology
        • Assessing The Network with Common Security Tools
        • Monitoring and Logging Network Traffic
        • Configuring Firewall Interfaces with pfSense
        • Network Security Architecture Redesign
        • Configuring a VPN Server with pfSense
      • Network Security, Firewalls, and VPNs
        • Chapter 1: Fundamentals of Network Security
        • Chapter 2: Network Security Threats
        • Chapter 3: Common Network Topologies and Infrastructures
        • Chapter 4: Network Design Considerations
        • Chapter 5: Firewall Fundamentals
        • Chapter 6: Firewall Implementation
        • Chapter 7: Firewall Deployment Considerations
        • Chapter 8: Configuring Firewalls
        • Chapter 9: VPN Fundamentals
        • Chapter 10: VPN Management
        • VPN Policy
        • VPN Deployment Plan
        • Chapter 14: Best Practices for Network Security Management
    • Django Projects
      • Django Worship Team Website Development Plan
      • Django LMS Project Plan
  • Documents for Sphinx Site Quick Access
    • Using Sphinx
    • Programming Langauges Integration
      • C++ Programs
        • C++ Intro
      • Python Programs
  • Contact
Back to top
View this page

2- Binary Search¶

#include <iostream>
#include <vector>
using namespace std;

// Recursive Binary Search function
// Parameters:
// - arr: the sorted array we’re searching
// - left: the starting index of the current search range
// - right: the ending index of the current search range
// - target: the number we’re looking for
int binarySearchRecursive(const vector<int>& arr, int left, int right, int target) 
{
    // Base case: if the search range is invalid (left > right), target is not found
    if (left > right)
        return -1; // return -1 to signal "not found"

    // Find the middle index of the current range
    int mid = left + (right - left) / 2;

    // Case 1: If the middle element matches the target, we found it
    if (arr[mid] == target)
        return mid;

    // Case 2: If the target is larger than the middle element, it must be in the right half of the array
    else if (arr[mid] < target)
        return binarySearchRecursive(arr, mid + 1, right, target);

    // Case 3: If the target is smaller than the middle element, it must be in the left half of the array
    else
        return binarySearchRecursive(arr, left, mid - 1, target);
}

int main() 
{
    // Define a sorted array (binary search only works on sorted data)
    vector<int> arr = {1, 3, 5, 7, 9, 11, 13};

    // Target value to search for
    int target = 7;

    // Call recursive binary search on the full array (indexes 0 → size-1)
    int result = binarySearchRecursive(arr, 0, arr.size() - 1, target);

    // Check result: if -1, not found; otherwise print index
    if (result != -1)
        cout << "Found " << target << " at index " << result << endl;
    else
        cout << target << " not found" << endl;

    return 0;
}
Next
3- Dynamic Data Structures
Previous
Dynamic Data Structures
Copyright © 2026, BMM
Made with Sphinx and @pradyunsg's Furo
🌐