Skip to content

Instantly share code, notes, and snippets.

@tcw165
Last active May 16, 2016 03:19
Show Gist options
  • Save tcw165/4234de9c076c443286151edc03cb6e2e to your computer and use it in GitHub Desktop.
Save tcw165/4234de9c076c443286151edc03cb6e2e to your computer and use it in GitHub Desktop.
/**
* Insert a new {@link Whatever} instance in ascent/descent order.
*/
public void insertByWhateverOrder(List<Whatever> whatevers, Whatever one) {
// The complexity is O(nlog(n)). The list needs to be already sorted in
// natural sorting order. Searching in an unsorted array has an undefined
// result.
int position = Collections.binarySearch(whatevers, one);
if (position < 0) {
whatevers.add(-position - 1, one);
} else {
whatevers.add(position, one);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment