Skip to content

Instantly share code, notes, and snippets.

@x100ex
Created June 2, 2017 13:34
Show Gist options
  • Save x100ex/e22dbeedcc06adbe633ef47f2e2336dc to your computer and use it in GitHub Desktop.
Save x100ex/e22dbeedcc06adbe633ef47f2e2336dc to your computer and use it in GitHub Desktop.
linear_4.c
// https://schani.wordpress.com/2010/04/30/linear-vs-binary-search/
static int
linear_4 (const int *arr, int n, int key) {
int i = 0;
while (i + 3 < n) {
if (arr [i + 0] >= key) return i + 0;
if (arr [i + 1] >= key) return i + 1;
if (arr [i + 2] >= key) return i + 2;
if (arr [i + 3] >= key) return i + 3;
i += 4;
}
while (i < n) {
if (arr [i] >= key)
break;
++i;
}
return i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment