Generic Binary Search in C#

前端 未结 4 899
醉酒成梦
醉酒成梦 2020-12-16 22:25

Below is my Generic Binary Search. It works okay with the integers type array (it finds all the elements in it). But the problem arises when I use a string array to find any

相关标签:
4条回答
  • 2020-12-16 23:06

    The two lines are suspect:

    high = mid + 1
    low = mid + 1
    

    Hmm. Look at the offsets. Of course this is well documented Binary Search Algorithm on Wikipedia. You also do extra work. Examine the pseudo-code and examples closely.

    0 讨论(0)
  • 2020-12-16 23:13

    A binary search requires that the input be sorted. How is "b, a, ab, abc, c" sorted? It does not appear to be sorted on any obvious sort key. If you are trying to search unsorted data you should be using a hash set, not a binary search on a list.

    Also, your calculation of midpoint is subtly wrong because the addition of high + low can overflow. It then becomes a negative number, which is divided by two.

    This is extremely unlikely for realistically-sized arrays but it is entirely possible that you'll want to use this algorithm someday for data types that support indexing with large integers, like a memory-mapped file of sorted data.

    The best practice for writing a binary search algorithm is to do (high - low) / 2 + low when calculating the midpoint, because that stays in range the whole time.

    0 讨论(0)
  • 2020-12-16 23:20

    pst Your advice really worked. :) this code is working for both int and string.

        public static int BinarySearch<T>(T[] array, T searchFor, Comparer<T> comparer)
        {
            int high, low, mid;
            high = array.Length - 1;
            low = 0;
            if (array[0].Equals(searchFor))
                return 0;
            else if (array[high].Equals(searchFor))
                return high;
            else
            {
                while (low <= high)
                {                   
                    mid = (high + low) / 2;
                    if (comparer.Compare(array[mid], searchFor) == 0)                   
                        return mid;                    
                    else if (comparer.Compare(array[mid], searchFor) > 0)                    
                        high = mid - 1;                    
                    else                    
                        low = mid + 1;                  
                }
                return -1;                
            }                 
        }
    
    0 讨论(0)
  • 2020-12-16 23:24

    //Binary search recursive method

        public void BinarySearch(int[] input,int key,int start,int end)
        {
            int index=-1;
            int mid=(start+end)/2;
            if (input[start] <= key && key <= input[end])
            {
                if (key < input[mid])
                    BinarySearch(input, key, start, mid);
                else if (key > input[mid])
                    BinarySearch(input, key, mid + 1, end);
                else if (key == input[mid])
                    index = mid;
                if (index != -1)
                    Console.WriteLine("got it at " + index);
            }
        }  
    
         int[] input4 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };   
         BinarySearch(input4, 1, 0, 8);
    
    0 讨论(0)
提交回复
热议问题