If you are able, you might consider changing the way you store and create data in the first place. Instead of adding elements to the array, create a complete binary tree (or something else) and work with it. This makes data manipulation a little slower, but it should not be as slow as re-sorting the data every time and will make your searches much faster.
source
share