在進行參數試錯時,通常將可能的參數由小到大排列一個個進行測試,這樣的測試順序很多時候不太合理,因此寫了一個按二分法遍歷順序排列的算法,通常能更快的找到合適的參數。代碼如下:
/************************************************* 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)