将查询过程一分为二,实现快速查询
template <typename T>
int BinarySearch(T &arr[], T key)
{
int low=0, mid=0, high = ArraySize(arr);
while(low <= high)
{
mid = (low + high) / 2;
if(arr[mid] == key)
return mid;
if(arr[mid] > key)
high = mid - 1;
if(arr[mid] < key)
low = mid + 1;
}
return -1;
}
调用方法
#property copyright "Copyright 2022, Author:阿龙."
#property link "https://www.guapit.com"
#property description "MT5智能交易编程课程"
#property description "QQ: 8199231"
#property version "1.00"
#include <GuapitPro/查询算法.mqh>
void OnStart()
{
int arr[] = {1,5,3,4,8,6,7,9,10};
printf("查询结果: %d",BinarySearch(arr,9));
}
结果:
2023.01.05 20:07:08.514 查询算法测试 (XAUUSD,H1) 查询结果: 7