在进行参数试错时,通常将可能的参数由小到大排列一个个进行测试,这样的测试顺序很多时候不太合理,因此写了一个按二分法遍历顺序排列的算法,通常能更快的找到合适的参数。代码如下:
/************************************************* Function: // BinarySort Description: // sort array according to the traversal sequence of dichotomy Input: // srcArray Output: // dstArray *************************************************/ void BinarySort(vector<int> srcArray, vector<int>& dstArray) { int begin = 0, end = srcArray.size()-1; int mid, tempBegin, tempEnd; vector<Point> intervalList; intervalList.push_back(Point(begin,end)); while(intervalList.size()>0) { begin = intervalList.front().x; end = intervalList.front().y; mid = (begin + end)/2; dstArray.push_back(srcArray[mid]); if(begin <= mid-1) { tempBegin = begin; tempEnd = mid - 1; intervalList.push_back(Point(tempBegin,tempEnd)); } if(mid+1 <= end) { tempBegin = mid+1; tempEnd = end; intervalList.push_back(Point(tempBegin,tempEnd)); } intervalList.erase(intervalList.begin()); } }
例如:
输入srcArray = [10](164,174,184,194,204,214,224,234,244,254)
输出dstArray = [10](204,174,234,164,184,214,244,194,224,254)