Skip to main content
deleted 418 characters in body
Source Link
Mike Nakis
  • 2k
  • 1
  • 12
  • 15

In C# both the System.Generic.List class and the System.Array class already haveThe best way to implement a public generic method for binary search is by invoking the BinarySearch() methods which work with Comparer delegates, or with objects implementingmethod of IComparableSystem.Array. They are known to work very well, and they perform actual search, meaning that they return the index of the item found, (or, if not found, the negated index at which it should be inserted,) instead of what your methods do, which is to simply answer the question of whether an item exists in an array.

SoBetter yet, why are you trying to re-invent this wheel?

(I would have added this asdon't implement such a comment rather than as an answermethod at all, but I see no 'comment' linkand call System.Array.BinarySearch() directly.)

In C# both the System.Generic.List class and the System.Array class already have generic BinarySearch() methods which work with Comparer delegates, or with objects implementing IComparable. They are known to work very well, and they perform actual search, meaning that they return the index of the item found, (or, if not found, the negated index at which it should be inserted,) instead of what your methods do, which is to simply answer the question of whether an item exists in an array.

So, why are you trying to re-invent this wheel?

(I would have added this as a comment rather than as an answer, but I see no 'comment' link.)

The best way to implement a public generic method for binary search is by invoking the BinarySearch() method of System.Array. (Better yet, don't implement such a method at all, and call System.Array.BinarySearch() directly.)

added 97 characters in body
Source Link
Mike Nakis
  • 2k
  • 1
  • 12
  • 15

In C# both the System.Generic.List class and the System.Array class already have generic BinarySearch() methods which work with Comparer delegates, or with objects implementing IComparable. They are known to work very well, and they perform actual search, meaning that they return the index of the item found, (or, if not found, the negated index at which it should be inserted,) instead of what your methods do, which is to simply answer the question of whether an item exists in an array.

So, why are you trying to re-invent this wheel?

(I would have added this as a comment rather than as an answer, but I see no 'comment' link.)

In C# both the System.Generic.List class and the System.Array class already have generic BinarySearch() methods which work with Comparer delegates, or with objects implementing IComparable. They are known to work very well, and they perform actual search, meaning that they return the index of the item found, (or, if not found, the negated index at which it should be inserted,) instead of what your methods do, which is to simply answer the question of whether an item exists in an array.

So, why are you trying to re-invent this wheel?

In C# both the System.Generic.List class and the System.Array class already have generic BinarySearch() methods which work with Comparer delegates, or with objects implementing IComparable. They are known to work very well, and they perform actual search, meaning that they return the index of the item found, (or, if not found, the negated index at which it should be inserted,) instead of what your methods do, which is to simply answer the question of whether an item exists in an array.

So, why are you trying to re-invent this wheel?

(I would have added this as a comment rather than as an answer, but I see no 'comment' link.)

Source Link
Mike Nakis
  • 2k
  • 1
  • 12
  • 15

In C# both the System.Generic.List class and the System.Array class already have generic BinarySearch() methods which work with Comparer delegates, or with objects implementing IComparable. They are known to work very well, and they perform actual search, meaning that they return the index of the item found, (or, if not found, the negated index at which it should be inserted,) instead of what your methods do, which is to simply answer the question of whether an item exists in an array.

So, why are you trying to re-invent this wheel?