int searchSection(int values[], int size, int value, int left, int right) { int l = left; int r = right; int middle = (left + right) / 2; if (size < 1) { return -1; } if (values[middle] == value) { printf("Found value!!\n"); return 0; } if (value < values[middle]) { // Search in left half r = middle - 1; searchSection(values, r - l + 1, value, l, r); }else if (value > values[middle]) { //Search in right half l = middle + 1; searchSection(values, r - l + 1, value, l, r); } return; }
void main(int argc, char* argv []) { int values[] = {1, 3, 5, 6, 8, 9, 48, 79, 82}; int size = sizeof(values)/sizeof(values[0]); int value = atoi(argv[1]); int left = 0; int right = size - 1; // Binary search algorithm int result = searchSection(values, size, value, left, right); printf("%d\n", result); }
// Constructor // begin of a profile section VSProfileLib::VSProfileLib(std::string name, bool profileGL) { int found; pTime w; sCurrLevel++; #if VSPL_CLOCK == VSPL_WIN_HIGH_PERFORMANCE_COUNTER QueryPerformanceFrequency(&sFreq); #endif GetTicks(&w); // create new level if (sCurrLevel == sTotalLevels) { sLevels[sCurrLevel].cursor = -1; createNewSection(name, w, profileGL); // store the size of the largest section name int aux = name.size() ; if (aux > sDisp) sDisp = aux; sTotalLevels++; } else { // search for name and parent found = searchSection(name); if (found != -1) updateSection(found, w); else { // create new section inside current level createNewSection(name, w, profileGL); // store the size of the largest section name // for report formatting purposes int aux = name.size() ; if (aux > sDisp) sDisp = aux; } } }