我试图实现一个函数,该函数将查看数组的每个元素,并确定该特定元素是否大于一个INT且小于另一个INT。例如:
Return true if Arr[5] is >i && < u
我将此作为一种基本算法,但是我希望通过使用“分而治之”来创建更高效的代码片段。方法论,但是我在使用递归来计算它时遇到了问题,我所看到的所有例子只涉及一个比较点,而不是两个。任何人都可以了解情况。 (http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm)
我的原始代码(线性):
int SimpleSort(int length)
{
int A[] = {25,5,20,10,50};
int i = 25; //Less Than int
u = 2; //Greater Than
for(int Count = 0; Count < length; Count++) //Counter
{
if (A[Count] <= i && A[Count] >= u) //Checker
return true;
} return false;
}
我迄今为止所获得的示例代码(经过多个小时处理各种事情并使用不同的示例代码后没有运气:
int A[] = {5,10,25,30,50,100,200,500,1000,2000};
int i = 10; //Less Than
int u = 5; //Greater Than
int min = 1;
int max = length;
int mid = (min+max)/2;
if (i < A[mid] && u > A[mid])
{
min = mid + 1;
}
else
{
max = mid - 1;
}
Until i <= A1[mid] && u >= A1[mid])
如果这个问题不明确,我很抱歉,请问您是否需要我详细说明。
答案 0 :(得分:3)
假设您的输入向量总是排序,我认为这样的事情可能适合您。这是我能想到的最简单的形式,性能是O(log n):
bool inRange(int lval, int uval, int ar[], size_t n)
{
if (0 == n)
return false;
size_t mid = n/2;
if (ar[mid] >= std::min(lval,uval))
{
if (ar[mid] <= std::max(lval,uval))
return true;
return inRange(lval, uval, ar, mid);
}
return inRange(lval, uval, ar+mid+1, n-mid-1);
}
这使用隐含的范围差分;即,它总是使用两个值中的较低者作为下限,并且两者中的较高者作为上限。如果您的使用要求lval
和uval
的输入值被视为福音,那么任何调用lval > uval
应该返回false(因为它是不可能)您可以删除std::min()
和std::max()
扩展。在任何一种情况下,您都可以通过制作一个outter前端加载器并预先检查lval
和uval
的顺序来进一步提高性能,以便(a)如果需要绝对排序则立即返回false并{ {1}},或者(b)如果要求范围差异,则以正确的顺序预先确定lval和uval。下面将探讨这种外包装的例子:
lval > uval
我已将我认为你想要的那个留给了// search for any ar[i] such that (lval <= ar[i] <= uval)
// assumes ar[] is sorted, and (lval <= uval).
bool inRange_(int lval, int uval, int ar[], size_t n)
{
if (0 == n)
return false;
size_t mid = n/2;
if (ar[mid] >= lval)
{
if (ar[mid] <= uval)
return true;
return inRange_(lval, uval, ar, mid);
}
return inRange_(lval, uval, ar+mid+1, n-mid-1);
}
// use lval and uval as an hard range of [lval,uval].
// i.e. short-circuit the impossible case of lower-bound
// being greater than upper-bound.
bool inRangeAbs(int lval, int uval, int ar[], size_t n)
{
if (lval > uval)
return false;
return inRange_(lval, uval, ar, n);
}
// use lval and uval as un-ordered limits. i.e always use either
// [lval,uval] or [uval,lval], depending on their values.
bool inRange(int lval, int uval, int ar[], size_t n)
{
return inRange_(std::min(lval,uval), std::max(lval,uval), ar, n);
}
。有希望覆盖主要和边缘情况的单元测试以及结果输出。
inRange
输出结果:
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <iterator>
int main(int argc, char *argv[])
{
int A[] = {5,10,25,30,50,100,200,500,1000,2000};
size_t ALen = sizeof(A)/sizeof(A[0]);
srand((unsigned int)time(NULL));
// inner boundary tests (should all answer true)
cout << inRange(5, 25, A, ALen) << endl;
cout << inRange(1800, 2000, A, ALen) << endl;
// limit tests (should all answer true)
cout << inRange(0, 5, A, ALen) << endl;
cout << inRange(2000, 3000, A, ALen) << endl;
// midrange tests. (should all answer true)
cout << inRange(26, 31, A, ALen) << endl;
cout << inRange(99, 201, A, ALen) << endl;
cout << inRange(6, 10, A, ALen) << endl;
cout << inRange(501, 1500, A, ALen) << endl;
// identity tests. (should all answer true)
cout << inRange(5, 5, A, ALen) << endl;
cout << inRange(25, 25, A, ALen) << endl;
cout << inRange(100, 100, A, ALen) << endl;
cout << inRange(1000, 1000, A, ALen) << endl;
// test single-element top-and-bottom cases
cout << inRange(0,5,A,1) << endl;
cout << inRange(5,5,A,1) << endl;
// oo-range tests (should all answer false)
cout << inRange(1, 4, A, ALen) << endl;
cout << inRange(2001, 2500, A, ALen) << endl;
cout << inRange(1, 1, A, 0) << endl;
// performance on LARGE arrays.
const size_t N = 2000000;
cout << "Building array of " << N << " random values." << endl;
std::vector<int> bigv;
generate_n(back_inserter(bigv), N, rand);
// sort the array
cout << "Sorting array of " << N << " random values." << endl;
std::sort(bigv.begin(), bigv.end());
cout << "Running " << N << " identity searches..." << endl;
for (int i=1;i<N; i++)
if (!inRange(bigv[i-1],bigv[i],&bigv[0],N))
{
cout << "Error: could not find value in range [" << bigv[i-1] << ',' << bigv[i] << "]" << endl;
break;
};
cout << "Finished" << endl;
return 0;
}
答案 1 :(得分:1)
如果您假设要对数组进行排序,那么它实际上非常简单。通过始终查看序列的左侧或右侧,您可以避免对数的复杂性:
#include <iterator>
template <typename Limit, typename Iterator>
bool inRange(Limit lowerBound, Limit upperBound, Iterator begin, Iterator end) {
if (begin == end) // no values => no valid values
return false;
Iterator mid = begin;
auto const dist = std::distance(begin,end);
std::advance(mid,dist/2); // mid might be equal to begin, if dist == 1
if (lowerBound < *mid && *mid < upperBound)
return true;
if (dist == 1) // if mid is invalid and there is only mid, there is no value
return false;
if (*mid > upperBound)
return inRange(lowerBound, upperBound, begin, mid);
std::advance(mid,1); // we already know mid is invalid
return inRange(lowerBound, upperBound, mid, end);
}
您可以使用以下命令为普通数组调用此方法:
inRange(2,25,std::begin(A),std::end(A));
答案 2 :(得分:0)
据我所知,对你的具体问题使用分而治之 不会产生优势。但是,至少在您的示例中,输入是 排序;应该可以通过跳过值来改善一点,直到达到你的下限。