binarysearch的问题(使用新值计算first,last,mid)

以下代码属于二进制搜索算法。用户在textbox1中输入数字,并在textbox2中输入他想要使用binarysearch查找的数字。 现在我有一个问题,我希望当mid,first,last的值再次改变它时会开始function.i意味着例如:when last = mid-1;函数再次开始并用last的新值计算(我在代码中注释了解更多解释) 谢谢
 private void button1_Click(object sender, EventArgs e)
    {
        string strsearchnums = textBox2.Text;
        int result = binarysearch(strsearchnums);
        textBox14.Text = result.ToString();
    }
    public int binarysearch(string strsearchnum)
    {
        string[] source = textBox1.Text.Split(',');
        int[] nums = new int[source.Length];
        for (int i = 0; i < source.Length; i++)
        {
            nums[i] = Convert.ToInt32(source[i]);
        }


        int searchnum = Convert.ToInt32(strsearchnum);
        int first = nums.First();

        int last = nums.Last();

        while (1 <= nums.Length - 1)
        {
            int mid = (int)Math.Floor(nums.Length / 2.0) - 1;

            if (nums[0]> nums[nums.Length-1])
            {
                break;
           }

            if (searchnum < nums[mid])
            {
                last = mid - 1;

         binarysearch(strsearchnum);       ///i thought i should do like this but this isnt correct it becomes stackoverflow    
            }
            if (searchnum > nums[mid])
            {
                first = mid + 1;
      binarysearch(strsearchnum);      ///i thought i should do like this but this isnt correct it becomes stackoverflow    
            }
            if (searchnum == nums[mid])
            {


              return nums[mid];


            }

        }

        return -1; 
    }
    
已邀请:
首先,当用户在文本框中输入数字时,这些数字可能不会被排序,因此您需要首先对数组进行排序,因为二进制搜索算法与排序的序列一起使用。
   1- Splitt the string with ',' and store in the int[] array.

  string[] strArray = textBox1.Text.split(new string[]{","}, StringSplitOptions.RemoveEmptyEntries);

    List<int> lstInts = new List<int>();      
   foreach(string str in strArray)
   {
      int j;
      int k=0;

      if(int.TryParse(str,out j))
        {
           lstInts.Add(j);
        }
    }
     int[] bsArray = lstInts.ToArray();

   2- Now Sort the Array.
      Array.Sort(bsArray);

   3- Now use Array.Binaryearch() , if you wanna use  framework implementation which is optimized also

      Array.BinarySearch(bsArray, valueToBeSearched)

     if you don't wanna use Framework implementation than follow below algorithm

   public int DoBinarySearchNoNRecursively(int[] array, int value)
    {
        int high = array.Length - 1;

        int low = 0;

        while (low < high)
        {
            int mid = low + (high - low) / 2;

            if (value == array[mid])
                return mid;
            else if (value > array[mid])
                low = mid + 1;
            else if (value < array[mid])
                high = mid;

        }

        return -1;
    }
    
第一个问题是你永远不会改变
strsearchnum
的值 - 所以你总是得到一个Stackoverflow异常,因为它是无限递归。通常,对于递归二进制搜索,您希望binarySearch方法传递您要查找的数字,最小值和最大值,并根据二进制搜索算法修改最小值和最大值。方法签名应该是这样的
int binarySearch(int min, int max, int num)
{

}
这应该可以帮助您入门。     
我更正了下面的代码并更正了:
 public int binarysearch(int searchnum)
    {
        string[] source = textBox1.Text.Split(',');
        int[] nums = new int[source.Length];
        for (int i = 0; i < source.Length; i++)
        {
            nums[i] = Convert.ToInt32(source[i]);
        }
        int first = 0;
        int last = nums.Length - 1;


        while (1 <= nums.Length)
        {
            int mid = (int)Math.Floor((first + last) / 2.0);
            if (first > last)
            {
                break;
            }
            if (searchnum < nums[mid])
            {
                last = mid - 1;
            }
            if (searchnum > nums[mid])
            {
                first = mid + 1;
            }
            if (searchnum == nums[mid])
            {
                return nums[mid];
            }
        }
        return -1;

    }
    
这就是BinarySearch的工作方式......   二进制搜索算法更多   比线性搜索更有效   算法,但它需要   数组被排序。
    // perform a binary search on the data      
     public int binarysearch(string strsearchnum)
{

    string[] source = textBox1.Text.Split(',');
    int[] data = new int[source.Length];
    for (int i = 0; i < source.Length; i++)
    {
        data[i] = Convert.ToInt32(source[i]);
    }


  int low = 0 ; // low end of the search area                
  int high = data.length - 1 ; // high end of the search area
  int middle = ( low + high + 1 ) / 2 ; // middle element    
  int location = -1; // return value; -1 if not found        

  do // loop to search for element
     {


    // if the element is found at the middle               
     if ( searchElement == data[ middle ] )                 
    location = middle; // location is the current middle

    // middle element is too high                      
    else if ( searchElement < data[ middle ] )         
    high = middle - 1 ; // eliminate the higher half
    else // middle element is too low                  
    low = middle + 1 ; // eliminate the lower half  

    middle = ( low + high + 1 ) / 2 ; // recalculate the middle
    } while ( ( low <= high ) && ( location == -1 ) );            

    return location; // return location of search key
   } // end method binarySearch                         
    

要回复问题请先登录注册