什么数据结构最适合使用搜索算法?

时间:2017-05-07 12:41:48

标签: c# arrays algorithm search collections

我有一个任务,我需要实现一个完全编写的搜索算法,根据月份的内容进行搜索,并显示来自不同数据文件的所有相应数据。

哪种数据结构最适合存储我的数据,以便我可以:

  1. 在其上应用二进制搜索算法。
  2. 以相应的方式存储数据。
  3. 看看我的数据当前是如何存储在数组中的:

    Now

    这就是我的目标:

    After

    正如您在Afters图像中看到的那样,我在Months列周围编辑了引号,这是为了清楚地表明我想根据Month的内容搜索数据。 (例如,当我使用算法搜索所有“1月”月份时,所有1月份的值都会与相应的数据一起打印出来。)

    我正在尝试使用以下条件存储数据:

    1. 来自不同阵列的值仍然相互对应。
    2. 我可以对数据应用搜索算法,并根据月份的内容搜索相应的数据。
    3. 我很难用简洁,易懂的方式来解释这个问题,我希望这不会太混乱。我几十个小时一直在努力解决这个问题,似乎无法从任何地方找到解决方案。

      这是我存储数组的方式:

        //Read in the files and put them into arrays.
              String[] Years = File.ReadAllLines(@"Data\Year_1.txt");
              String[] Months = File.ReadAllLines(@"Data\Month_1.txt");
              String[] Days = File.ReadAllLines(@"Data\Day_1.txt");
              String[] Times = File.ReadAllLines(@"Data\Time_1.txt");
              String[] Depths = File.ReadAllLines(@"Data\Depth_1.txt");
              String[] Latitudes = File.ReadAllLines(@"Data\Latitude_1.txt");
              String[] Longitudes = File.ReadAllLines(@"Data\Longitude_1.txt");
              String[] Magnitudes = File.ReadAllLines(@"Data\Magnitude_1.txt");
              String[] Regions = File.ReadAllLines(@"Data\Region_1.txt");
              String[] IRIS_IDs = File.ReadAllLines(@"Data\IRIS_ID_1.txt");
              String[] Timestamps = File.ReadAllLines(@"Data\Timestamp_1.txt");
      
      
              //Creating an array of months and adding corresponding data from other arrays. 
              string[] MonthsArray = new string[Days.Length];
              //Since all files have same number of items, using any array.Length will iterate through all the items.
              for (int i = 0; i < Days.Length; i++)
              {
                  MonthsArray[i] = string.Format("{0,10} {1,6} {2,6} {3,9} {4,7} {5,7} {6,7} {7,7} {8,29} {9,9} {10,9}", Months[i], Years[i], Days[i], Times[i], Magnitudes[i], Latitudes[i], Longitudes[i], Depths[i], Regions[i], IRIS_IDs[i], Timestamps[i]);
              }
              //Creating an array of Years and adding corresponding data from other arrays.
              string[] YearsArray = new string[Days.Length];
              //Since all files have same number of items, using any array.Length will iterate through all the items.
              for (int i = 0; i < Days.Length; i++)
              {
                  YearsArray[i] = string.Format("{0,6} {1,10} {2,6} {3,9} {4,7} {5,7} {6,7} {7,7} {8,29} {9,9} {10,9}", Years[i], Months[i], Days[i], Times[i], Magnitudes[i], Latitudes[i], Longitudes[i], Depths[i], Regions[i], IRIS_IDs[i], Timestamps[i]);
              }
      

      这是我的二进制搜索算法,它会计算重复项的出现次数,以便我可以打印出所有搜索到的值:

      public static int Binary_Search<T>(T[] data, int n, T Search, bool searchFirst, Comparer<T> comparer)
          {
              int low = 0;
              int high = n - 1;
              int result = -1;
              while (low <= high)
              {
                  int mid = (low + high) / 2;
                  if (comparer.Compare(data[mid], Search) == 0)
                  {
                      result = mid;
                      if (searchFirst)
                          high = mid - 1;
                      else
                          low = mid + 1;
                  }
                  else if (comparer.Compare(Search, data[mid]) < 0)
                      high = mid - 1;
                  else
                      low = mid + 1;
              }
              return result;
          }
      

      这是我对已经排序的数组执行二进制搜索并显示搜索值的方法:

       Console.WriteLine("\nEnter the name of the Month to display all the seismic Data in that month.");
                      Console.Write("Month: \n");
                      var Search1 = Console.ReadLine();
                      int firstIndex1 = Binary_Search(MonthsArray, MonthsArray.Length, Search1, true, Comparer<string>.Default);
                      if (firstIndex1 == -1)
                      {
                          Console.WriteLine("Count of {0} is {1}", Search1, 0);
                      }
                      else
                      {
                          int lastIndex = Binary_Search(MonthsArray, MonthsArray.Length, Search1, false, Comparer<string>.Default);
                          //Count the occurrence of duplicate elements and display the searched value per each occurrence.
                          int occurence = lastIndex - firstIndex1 + 1;
                          for (int i = 0; i < occurence; i++)
                          {
                              Console.WriteLine(MonthsArray);
                          }
                          Console.WriteLine("Number of searched items found in the array: {0}", occurence);
                      }
                      break;
                  }
              break;
      

      数据类以及相应的数据类型。

          public class SeismicData
          {
              public int Year { get; set; }
              public string Month { get; set; }
              public int Day { get; set; }
              public double Time { get; set; }
              public double Depth { get; set; }
              public double Latitude { get; set; }
              public double Longitude { get; set; }
              public double Magnitude { get; set; }
              public string Region { get; set; }
              public int IRIS_ID { get; set; }
              public int TimeStamp { get; set; }
          }
      

      我的QuickSort算法:

         //QuickSort method for ascending arrays. Takes in any data type array and sorts it.
          public static void QuickSort<T>(T[] data)
          {
              Quick_Sort(data, 0, data.Length - 1, Comparer<T>.Default);
          }
          //Second QuickSort method for case 2 for Descending the array.
          public static void QuickSort<T>(T[] data, Comparer<T> comparer)
          {
              Quick_Sort(data, 0, data.Length - 1, comparer);
          }
          //QuickSort algorithm using comparer
          public static void Quick_Sort<T>(T[] array, int left, int right, Comparer<T> comparer)
          {
              int i, j;
              T pivot, temp;
              i = left;
              j = right;
              pivot = array[(left + right) / 2];
              do
              {
                  while ((comparer.Compare(array[i], pivot) < 0) && (i < right)) i++;
                  while ((comparer.Compare(pivot, array[j]) < 0) && (j > left)) j--;
                  if (i <= j)
                  {
                      temp = array[i];
                      array[i] = array[j];
                      array[j] = temp;
                      i++;
                      j--;
                  }
              } while (i <= j);
              if (left < j) Quick_Sort(array, left, j, comparer);
              if (i < right) Quick_Sort(array, i, right, comparer);
          }
      

0 个答案:

没有答案