Created
March 29, 2019 13:06
-
-
Save scerelli/10e647af7728109a5eb511f84d1f8dad to your computer and use it in GitHub Desktop.
Just an implementation in pseudo of the Divide-and-conquer algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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