Last active
August 6, 2020 16:08
-
-
Save LeoPFreitas/01115376eb24b27a66acc7042d982584 to your computer and use it in GitHub Desktop.
Simple linear search algorithm for searching a value in sorted arrays.
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
public static int searchInSortedArray(int[] array, int value) { | |
int index = -1; | |
for (int i = 0; i < array.length; i++) { | |
if (array[i] == value) { | |
index = i; | |
break; | |
} else if (array[i] > value) { | |
break; | |
} | |
} | |
return index; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment