C uses binary lookup method to judge specified characters

  • 2021-10-16 02:26:13
  • OfStack

In this paper, an example is given to describe the method of judging specified characters by C # using 2-point search method. Share it for your reference, as follows:


private int sort_init(ref string[] chars, string str) // Array initialization 
{
  string[] temp = str.Split(' ');
  //temp.
  chars = new string[temp.Count()];
  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 (ch.Length == 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) // Determine whether it is a specified character 
{
   int end = stopChar.Length - 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 readers interested in C # related content, please check the topics on this site: "Summary of XML File Operation Skills in C #", "C # Common Control Usage Tutorial", "WinForm Control Usage Tutorial", "C # Data Structure and Algorithm Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"

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


Related articles: