2016-12-17 37 views
0

私は文字を読み取るJsonの実装を持っているので、名前は配列に入り、次にArray.BinarySearchを使用して要素の位置を取得します。配列からのバイナリ検索文字列(jsonを含む)

私はバイナリサーチが自分自身のものであることを援助する方法を研究しています。私は論理的に検索のために入力された文字列名で何をするのか見苦しいです。

Array.BinarySearchを使用する代わりに、アルゴリズムとは別のメソッドが必要です。

アドバイスや戦略はありますか? :)

例:

 /* json array implimented, manu printed etc... before this point,  */ 



    static void FindCharacters(Characters[] characters) 
    { 
     Characters result = new Characters(); 

     string userInput = Console.ReadLine(); 

     string[] name = new string[10000]; 



     Console.Write("Search Name : "); 
     string searchKeyword = Console.ReadLine(); 

     if (userInput.ToLower() == "name") 
     { 

      name = characters.Select(m => m.Name).ToArray(); 

     Array.Sort(name); 
     Sorting.Sort(characters, searchKeyword); 


     var tmp = BinarySearch(name, searchKeyword); 

     if (tmp < 0) 
     { 
      Console.WriteLine("No data found!"); 
      return; 
     } 
     else 
     { 
      result = characters[tmp]; 

      CharacterPrint(result); 
     } 
      //result = characters[tmp]; //Convert.ToInt32(tmp) 
      //CharacterPrint(result); 

}

public static int BinarySearch(int[] name, int item) 
    { 

     int min = 0; 
     int N = name.Length; 
     int max = N - 1; 
     do 
     { 
      int mid = (min + max)/2; 
      if (item > name[mid]) 
       min = mid + 1; 
      else 
       max = mid - 1; 
      if (name[mid] == item) 
       return mid; 
      //if (min > max) 
      // break; 
     } while (min <= max); 
     return -1; 
    } 
+0

あなたは 'name.CompareTo(otherName)を使用することができます。あなたはint型と一緒に行くしているとして'文字列と行動の同じ種類を取得します。 (-1は低い、1は高い、0は等しいことを意味します) – Abion47

+0

こんにちは、以前、Array.BinarySearchについて私を助けてくれました。私はname.Compare(otherName)を試しています。配列内の位置がわからない場合、どのように文字列名を見つけることができるのか理解できません。 – seems

+0

おそらく私はその質問を理解していないでしょう。あなたは、int配列のための働くメソッドを持っていて、それが文字列配列のために働くようにしようとしていますか? – Abion47

答えて

0

あなたintソリューションは、文字列のために完全に正常に動作します。実際には、ちょうどカップルラインを微調整することで、それはIComparableを実装する任意のデータ型のために働くだろう:あなたは上記の変更が反映さ見ることができます

string[] names = // ... 
string name = //... 

// Explicit calling 
int idx = BinarySearch<string>(names, name); 

// Implicit calling 
// The following option works because the C# compiler can tell you are 
// using two values of type string and inserts the correct generic 
// type for you 
int idx = BinarySearch(names, name); 

public static int BinarySearch<T>(T[] name, T item) 
    where T : IComparable<T> 
{ 

    int min = 0; 
    int N = name.Length; 
    int max = N - 1; 
    do 
    { 
     int mid = (min + max)/2; 
     int t = item.CompareTo(name[mid]);  // Temp value holder 
     if (t > 0)        // item > name[mid] 
      min = mid + 1; 
     else if (t < 0)       // item < name[mid] 
      max = mid - 1; 
     else         // item == name[mid] 
      return mid; 
     //if (min > max) 
     // break; 
    } while (min <= max); 
    return -1; 
} 

あなたはこのようにそれを呼び出すことができます既定の比較演算子(たとえば、 "<"、 ">"、 "==")を等価なCompareToの値に置き換える方法余分な変数tは、オブジェクト上に重複してCompareToを呼び出すのを避けるためです。

CompareToが動作する方法は、呼び出し元のオブジェクトを受け取り、渡されたオブジェクトと比較することです。渡されたオブジェクトがソートされたリストの呼び出し元オブジェクトの前に表示される場合、メソッドは-1を返します。後に表示される場合は、1を返します。同じ場合は0を返します。

はこの説明のために、次の例を参照してください:

// The following values are compared based on standard lexical alphabetical order 

a.CompareTo(b); // "b" would appear after "a", so this returns 1 
c.CompareTo(b); // "b" would appear before "c", so this returns -1 
b.CompareTo(b); // "b" and "b" are the same value, so this returns 0