Last active
May 16, 2016 03:19
-
-
Save tcw165/4234de9c076c443286151edc03cb6e2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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