Share binary search c++

thephucdo

New member
## Tìm kiếm nhị phân trong C ++

Tìm kiếm nhị phân là một thuật toán phân chia và chinh phục tìm thấy vị trí của giá trị mục tiêu trong một mảng được sắp xếp.Nó hoạt động bằng cách liên tục chia mảng làm đôi cho đến khi tìm thấy giá trị mục tiêu.

Độ phức tạp của thời gian của tìm kiếm nhị phân là O (log n), trong đó n là kích thước của mảng.Điều này có nghĩa là thời gian chạy của thuật toán phát triển logarit theo kích thước của dữ liệu đầu vào.Điều này làm cho tìm kiếm nhị phân trở thành một thuật toán rất hiệu quả để tìm các giá trị trong các bộ dữ liệu lớn.

Sau đây là một ví dụ về cách tìm kiếm nhị phân hoạt động trong C ++:

`` `C ++
#include <Istream>

sử dụng không gian tên STD;

int nhị phân nghiên cứu (int mảng [], int n, int target) {
// Khởi tạo các con trỏ trái và phải
int trái = 0;
int right = n - 1;

// Trong khi con trỏ bên trái nhỏ hơn hoặc bằng con trỏ bên phải
while (trái <= phải) {
// Tính chỉ số giữa
int mid = (trái + phải) / 2;

// Nếu giá trị đích bằng với phần tử ở chỉ số giữa,
// Sau đó trả về chỉ số giữa
if (mảng [mid] == target) {
trở lại giữa;
}

// Nếu không, nếu giá trị đích nhỏ hơn phần tử ở chỉ mục giữa,
// sau đó cập nhật con trỏ bên trái lên phần tử tiếp theo
khác if (target <mảng [mid]) {
Phải = giữa - 1;
}

// Nếu không, nếu giá trị đích lớn hơn phần tử ở chỉ số giữa,
// Sau đó, cập nhật con trỏ phù hợp với phần tử trước đó
khác {
trái = mid + 1;
}
}

// Nếu giá trị đích không được tìm thấy trong mảng, return -1
trả lại -1;
}

int main () {
// Tạo một mảng số nguyên
int mảng [] = {1, 3, 5, 7, 9};

// Tìm vị trí của giá trị mục tiêu 5 trong mảng
int index = BinarySearch (mảng, sizeof (mảng) / sizeof (mảng [0]), 5);

// In chỉ mục của giá trị mục tiêu
cout << "Chỉ số của giá trị đích là:" << index << endl;

trả lại 0;
}
`` `

## hashtags

* #tìm kiếm nhị phân
* #C ++
* #algorithms
* #cấu trúc dữ liệu
* #tìm kiếm
=======================================
## Binary Search in C++

Binary search is a divide-and-conquer algorithm that finds the position of a target value within a sorted array. It works by repeatedly dividing the array in half until the target value is found.

The time complexity of binary search is O(log n), where n is the size of the array. This means that the algorithm's runtime grows logarithmically with the size of the input data. This makes binary search a very efficient algorithm for finding values in large datasets.

The following is an example of how binary search works in C++:

```c++
#include <iostream>

using namespace std;

int binarySearch(int arr[], int n, int target) {
// Initialize the left and right pointers
int left = 0;
int right = n - 1;

// While the left pointer is less than or equal to the right pointer
while (left <= right) {
// Calculate the middle index
int mid = (left + right) / 2;

// If the target value is equal to the element at the middle index,
// then return the middle index
if (arr[mid] == target) {
return mid;
}

// Otherwise, if the target value is less than the element at the middle index,
// then update the left pointer to the next element
else if (target < arr[mid]) {
right = mid - 1;
}

// Otherwise, if the target value is greater than the element at the middle index,
// then update the right pointer to the previous element
else {
left = mid + 1;
}
}

// If the target value is not found in the array, return -1
return -1;
}

int main() {
// Create an array of integers
int arr[] = {1, 3, 5, 7, 9};

// Find the position of the target value 5 in the array
int index = binarySearch(arr, sizeof(arr) / sizeof(arr[0]), 5);

// Print the index of the target value
cout << "The index of the target value is: " << index << endl;

return 0;
}
```

## Hashtags

* #binary-search
* #C++
* #algorithms
* #data-structures
* #search
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top