Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Tìm kiếm Tam phân

 Lưu đồ thuật toán:

 Thuật toán:

#include <iostream>

#include <vector>

using namespace std;

int Ternary_search(vector<int>& arr, int target) {

int left = 0;

int right = arr.size() - 1;

while (left <= right) {

int mid = left + (right - left) / 2;

if (arr[mid] == target) {

return mid;

else if (arr[mid] > target) {


right = mid - 1;

else {

left = mid + 1;

return -1;

int main() {

int t; cin >>t;

while(t--){

vector<int> arr = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};

int target; cin >> target;

int index = Ternary_search(arr, target);

if (index != -1) {

cout << index;

else {

cout << "No";

cout << endl;

return 0;

You might also like