Skip to content

Instantly share code, notes, and snippets.

@derinbay
Created June 11, 2019 10:12
Show Gist options
  • Save derinbay/c46c85d58802566ac3bed126aa36430a to your computer and use it in GitHub Desktop.
Save derinbay/c46c85d58802566ac3bed126aa36430a to your computer and use it in GitHub Desktop.
private static boolean hasSortedBlocks(Iterable<Double> iterable, Comparator comparator) {
Iterator<Double> it = iterable.iterator();
if (it.hasNext()) {
Double prev = it.next();
Double next;
Double secondNext = null;
Double thirdNext = null;
while (it.hasNext()) {
next = it.next();
if (compare(comparator, prev, next) && it.hasNext()) {
secondNext = it.next();
if (compare(comparator, next, secondNext) && it.hasNext()) {
thirdNext = it.next();
if (compare(comparator, secondNext, thirdNext)) {
return false;
}
}
}
if (thirdNext != null) {
prev = thirdNext;
} else if (secondNext != null) {
prev = secondNext;
} else {
prev = next;
}
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment