Skip to content

Instantly share code, notes, and snippets.

@scerelli
Created March 29, 2019 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scerelli/10e647af7728109a5eb511f84d1f8dad to your computer and use it in GitHub Desktop.
Save scerelli/10e647af7728109a5eb511f84d1f8dad to your computer and use it in GitHub Desktop.
Just an implementation in pseudo of the Divide-and-conquer algorithm
int elementToFind // questo e' il numero che vuoi cercare nell'array, nel tuo caso lo mette l'utente
array data = [...some data] // qua ci metti il tuo array con tutta la roba
int mid;
int low = 0;
int high = data.length - 1;
while (high - low > 1) {
mid = Math.floor((low + high) / 2);
if (data[mid] < ) {
low = mid;
} else {
high = mid;
}
}
if (
elementToFind - data[low] <= data[high] - elementToFind
) {
return data[low];
}
return data[high];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment