How to perform binary search on NSArray?

前端 未结 5 674
失恋的感觉
失恋的感觉 2020-12-09 11:06

What is the simplest way to do a binary search on an (already) sorted NSArray?

Some potential ways I have spotted so far include:

  1. The use

5条回答
  •  独厮守ぢ
    2020-12-09 11:31

    1 and 2 will both work. #2 is probably easier; it certainly doesn't make sense for that method to do anything other than a binary search (if the range is above a certain size, say). You could verify on a large array that it only does a small number of comparisons.

提交回复
热议问题