Skip to content

Instantly share code, notes, and snippets.

@LeoPFreitas
Last active August 6, 2020 16:08
Show Gist options
  • Save LeoPFreitas/01115376eb24b27a66acc7042d982584 to your computer and use it in GitHub Desktop.
Save LeoPFreitas/01115376eb24b27a66acc7042d982584 to your computer and use it in GitHub Desktop.
Simple linear search algorithm for searching a value in sorted arrays.
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