void binarySearchIterativeModified(int a[], int n, int x) {
int i, j, k, midOdd, midEven;
i = 0;
j = n/2;
while(i <= j) {
k = (i+j) / 2;
midOdd = (2*k) + 1;
if(a[midOdd] == x) {
printf("%d is found at %d index position.", x, midOdd);
return;
} else if(a[midOdd] < x) {
i = k + 1;
} else {
j = k - 1;
}
}
/* The elements in odd indices are already sorted in increasing order.
Above loop searches for elements in the odd indices. If found, it
prints the corresponding index and returns. */
i = 0;
j = n/2;
while(i <= j) {
k = (i+j) / 2;
midEven = (2*k);
if(a[midEven] == x) {
printf("%d is found at %d index position.", x, midEven);
return;
} else if(a[midEven] < x) {
j = k - 1;
} else {
i = k + 1;
}
}
/* The elements in even indices are already sorted in decreasing order.
Above loop searches for elements in the even indices. If found, it
prints the corresponding index and returns. */
printf("%d is not found anywhere.", x);
return;
/* If element is not found either in odd or even indices, simply print
error message and return. */
}