Skip to content

Instantly share code, notes, and snippets.

@MichaelEstes
Created December 11, 2015 21:10
Show Gist options
  • Save MichaelEstes/1d90326266767d3e53b7 to your computer and use it in GitHub Desktop.
Save MichaelEstes/1d90326266767d3e53b7 to your computer and use it in GitHub Desktop.
Chop attempt 1:
int chop(int intToFind, int arrayToSearch[], int length){
int index = length / 2;
while (arrayToSearch[index] != intToFind){
if (arrayToSearch[index] > intToFind){
length = index;
index /= 2;
}
else if (arrayToSearch[index] < intToFind){
index = (index + length) / 2;
}
if (index >= (length - 1) || index == 0){
return -1;
}
}
return index;
}
Chop attempt 2:
bool chop(int intToFind, int arrayToSearch[], int length, int & foundIndex){
int index = length - 1;
if (arrayToSearch[index] < intToFind || arrayToSearch[0] > intToFind)
return false;
while (true){
steps++;
if (arrayToSearch[index] > intToFind){
length = index;
index /= 2;
} else {
index = (index + length) / 2;
}
if (arrayToSearch[index] == intToFind){
foundIndex = index;
return true;
} else if (arrayToSearch[index - 1] == intToFind){
foundIndex = index - 1;
return true;
} else if (arrayToSearch[index + 1] == intToFind){
foundIndex = index + 1;
return true;
}
else if (intToFind < arrayToSearch[index + 1] && intToFind > arrayToSearch[index - 1]){ return false; }
else if (index >= (length - 1) || index == 0){ return false; }
}
return false;
}
Chop attempt 3:
int chop(int intToFind, int arrayToSearch[], int min, int max, int length){
int index = ((min + max) % 2) == 0 ? ((min + max) / 2) : (((min + max) / 2) + 1);
if (arrayToSearch[index] == intToFind){ return index; }
else if (arrayToSearch[index - 1] == intToFind){ return index - 1; }
else if (arrayToSearch[index + 1] == intToFind){ return index + 1; }
else if (intToFind < arrayToSearch[index + 1] && intToFind > arrayToSearch[index - 1]){ return -1; }
else if (index == max){ return -1; }
if (intToFind < arrayToSearch[index]){
chop(intToFind, arrayToSearch, min, (index / 2), length);
}
else if (max < length && intToFind > arrayToSearch[max / 2]){
chop(intToFind, arrayToSearch, ((min + length) / 2), ((max + length) / 2), length);
}
}
Chop attempt 4:
bool chop(int intToFind, int arrayToSearch[], int length, vector<int> & foundIndices){
int index = length - 1;
if (arrayToSearch[index] < intToFind || arrayToSearch[0] > intToFind)
return false;
while (true){
if (arrayToSearch[index] > intToFind){
length = index;
index /= 2;
}
else { index = (index + length) / 2; }
if (arrayToSearch[index - 1] == intToFind){ index--; }
else if (arrayToSearch[index + 1] == intToFind){ index++; }
if (arrayToSearch[index] == intToFind){
int temp = index;
foundIndices.push_back(index);
while (temp - 1 >= 0 && arrayToSearch[temp - 1] == intToFind){
temp--;
foundIndices.push_back(temp);
}
temp = index;
while (temp + 1 < length && arrayToSearch[temp + 1] == intToFind){
temp++;
foundIndices.push_back(temp);
}
return true;
}
else if (intToFind < arrayToSearch[index + 1] && intToFind > arrayToSearch[index - 1]){ return false; }
else if (index >= (length - 1) || index == 0){ return false; }
}
return false;
}
Chop attempt 5:
bool chop(int intToFind, int arrayToSearch[], int min, int max, int length, vector<int> & foundIndices){
steps++;
int index = ((min + max) % 2) == 0 ? ((min + max) / 2) : (((min + max) / 2) + 1);
if (arrayToSearch[index] == intToFind){
int temp = index;
foundIndices.push_back(index);
while (arrayToSearch[temp - 1] == intToFind){
temp--;
foundIndices.push_back(temp);
}
temp = index;
while (arrayToSearch[temp + 1] == intToFind){
temp++;
foundIndices.push_back(temp);
}
return true;
}
else if (intToFind < arrayToSearch[index + 1] && intToFind > arrayToSearch[index - 1]){ return false; }
else if (index == max){ return false; }
if (intToFind < arrayToSearch[index]){
chop(intToFind, arrayToSearch, min, (index / 2), length, foundIndices);
}
else if (max < length && intToFind > arrayToSearch[max / 2]){
chop(intToFind, arrayToSearch, ((min + length) / 2), ((max + length) / 2), length, foundIndices);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment