title: Applying Binary Search to Program
body: <p>I currently have a program in which I read in files and sort using the bubble sort. However I need to implement a function that allows me to search through any of the files for a value. The algorithm I want to use is the Binary Search but I am unsure in how to implement this. </p>
  
  <p>How would I make it so that I could choose a file to search through for a value which I enter? </p>
  
  <p>Code used to read in files and allow to be sorted ( No help needed with sorting them):</p>
  
  <pre><code>var fileData = GetFileData(@"Data1/Day_1.txt", @"Data1/Depth_1.txt", 
      @"Data1/IRIS_ID_1.txt", @"Data1/Latitude_1.txt", @"Data1/Longitude_1.txt", 
      @"Data1/Magnitude_1.txt", @"Data1/Month_1.txt", @"Data1/Region_1.txt", 
      @"Data1/Time_1.txt", @"Data1/Timestamp_1.txt", @"Data1/Year_1.txt"); 
  </code></pre>
  
  <p>Above part is from the Main method</p>
  
  <pre><code>public class FileData
  {
      public int File1Value { get; set; }
      public decimal File2Value { get; set; }
      public int File3Value { get; set; }
      public decimal File4Value { get; set; }
      public decimal File5Value { get; set; }
      public decimal File6Value { get; set; }
      public string File7Value { get; set; }
      public string File8Value { get; set; }
      public float File9Value { get; set; }
      public int File10Value { get; set; }
      public int File11Value { get; set; }
  
      public override string ToString()
      {
          return String.Format($"{File1Value}|{File2Value}|{File3Value}|{File4Value}|{File5Value}|{File6Value}|{File7Value}|{File8Value}|{File9Value}|{File10Value}|{File11Value}");
      }
  }
  
  public static FileData[] GetFileData(string firstFilePath, string secondFilePath, 
      string thirdFilePath, string fourthFilePath, string fifthFilePath, 
      string sixthFilePath, string seventhFilePath, string eigthFilePath, 
      string ninthFilePath, string tenthFilePath, string eleventhFilePath)
  {
  
      int intHolder = 0;
      decimal decHolder = 0;
      float time = 0;
  
      // Get a list of ints from the first file
      var fileOneValues = File
          .ReadAllLines(firstFilePath)
          .Where(line =&gt; int.TryParse(line, out intHolder))
          .Select(v =&gt; intHolder)
          .ToArray();
  
      // Get a list of decimals from the second file
      var fileTwoValues = File
          .ReadAllLines(secondFilePath)
          .Where(line =&gt; decimal.TryParse(line, out decHolder))
          .Select(v =&gt; decHolder)
          .ToArray();
  
      var fileThreeValues = File
          .ReadAllLines(thirdFilePath)
          .Where(line =&gt; int.TryParse(line, out intHolder))
          .Select(v =&gt; intHolder)
          .ToArray();
  
      var fileFourValues = File
          .ReadAllLines(fourthFilePath)
          .Where(line =&gt; decimal.TryParse(line, out decHolder))
          .Select(v =&gt; decHolder)
          .ToArray();
  
      var fileFiveValues = File
          .ReadAllLines(fifthFilePath)
          .Where(line =&gt; decimal.TryParse(line, out decHolder))
          .Select(v =&gt; decHolder)
          .ToArray();
  
      var fileSixValues = File
         .ReadAllLines(sixthFilePath)
         .Where(line =&gt; decimal.TryParse(line, out decHolder))
         .Select(v =&gt; decHolder)
         .ToArray();
  
      var fileSevenValues = File
          .ReadAllLines(seventhFilePath)
          .ToArray();
  
      var fileEightValues = File
          .ReadAllLines(eigthFilePath)
          .ToArray();
  
      var fileNineValues = File
          .ReadAllLines(ninthFilePath)
          .Where(line =&gt; float.TryParse(line, out time))
          .Select(v =&gt; time)
          .ToArray();
  
      var fileTenValues = File
          .ReadAllLines(tenthFilePath)
          .Where(line =&gt; int.TryParse(line, out intHolder))
          .Select(v =&gt; intHolder)
          .ToArray();
  
      var fileElevenValues = File
          .ReadAllLines(eleventhFilePath)
          .Where(line =&gt; int.TryParse(line, out intHolder))
          .Select(v =&gt; intHolder)
          .ToArray();
  
      // I guess the file lengths should match, but in case they don't, 
      // use the size of the smaller one so we have matches for all items
      var numItems = Math.Min(fileOneValues.Count(), fileTwoValues.Count());
  
      // Populate an array of new FileData objects
      var fileData = new FileData[numItems];
      for (var index = 0; index &lt; numItems; index++)
      {
          fileData[index] = new FileData
          {
              File1Value = fileOneValues[index],
              File2Value = fileTwoValues[index],
              File3Value = fileThreeValues[index],
              File4Value = fileFourValues[index],
              File5Value = fileFiveValues[index],
              File6Value = fileSixValues[index],
              File7Value = fileSevenValues[index],
              File8Value = fileEightValues[index],
              //File9Value = fileNineValues[index],
              File10Value = fileTenValues[index],
              File11Value = fileElevenValues[index]
          };
      }
  
      return fileData;
  }
  </code></pre>
  
  <p>Code of Binary Search:</p>
  
  <pre><code>  static int BinarySearch_R(int key, FileData[] fileData, int low, int high)
  {
      if (low &gt; high) return -1;
      int mid = (low + high) / 2;
      if (key == fileData[mid])
      {
  
          return mid;
      }
      if (key &lt; fileData[mid])
      {
          return BinarySearch_R(key, fileData, low, mid - 1);
      }
      else
      {
  
          return BinarySearch_R(key, fileData, mid + 1, high);
      }
  }
  </code></pre>