SoFunction
Updated on 2025-03-06

C# Method to judge specified characters using binary search method

This article describes the method of C# using the binary search method to determine specified characters. Share it for your reference, as follows:

private int sort_init(ref string[] chars, string str) //Array initialization{
  string[] temp = (' ');
  //temp.
  chars = new string[()];
  int ndx = 0;
  int last_empty_positon = 0;
  foreach (string ch in temp)
  {
   ndx = last_empty_positon++;
   chars[ndx] = ch;
   if (ndx == 0) continue;
   if ( == 0) continue;
   while (chars[ndx].CompareTo(chars[ndx - 1]) < 0)
   {
    string s = chars[ndx];
    chars[ndx] = chars[ndx - 1];
    chars[ndx - 1] = s;
    ndx--;
    if (ndx == 0) break;
   }
  }
  return 1;
}

private bool isTheString(string str) //Discern whether it is a specified character{
   int end =  - 1;
   int begin = 0;
   while (end - begin > 1)
   {
    int mid = (end + begin) / 2;
    if (stopChar[mid].CompareTo(str) > 0)
     end = mid;
    else if (stopChar[mid].CompareTo(str) < 0)
     begin = mid;
    else
     return true;
   }
   if (stopChar[end].CompareTo(str) == 0 || stopChar[begin].CompareTo(str) == 0)
    return true;
   return false;
}

For more information about C# related content, please check out the topic of this site:Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.