C# 什么样的数据结构最适合使用搜索算法?
我有一个任务,我需要实现一个完整的书面搜索算法,根据月份的内容进行搜索,并显示来自不同数据文件的所有相应数据 什么样的数据结构最适合存储我的数据,以便我可以:C# 什么样的数据结构最适合使用搜索算法?,c#,arrays,algorithm,search,collections,C#,Arrays,Algorithm,Search,Collections,我有一个任务,我需要实现一个完整的书面搜索算法,根据月份的内容进行搜索,并显示来自不同数据文件的所有相应数据 什么样的数据结构最适合存储我的数据,以便我可以: 对其应用二进制搜索算法 以相应的方式存储数据 看看我的数据当前是如何存储在阵列中的: 这就是我的目标: 正如您可以从我编辑的月份列周围的引号后的图像中看到的,这是为了明确我想要根据月份的内容搜索数据。(例如,当我使用算法搜索所有“一月”月份时,所有一月值都会连同相应的数据一起打印出来) 我试图在以下条件下存储数据: 来自不同数组的值仍
//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]);
}
我的快速排序算法:
//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);
}
//升序数组的快速排序方法。接受任何数据类型数组并对其进行排序。
公共静态void快速排序(T[]数据)
{
快速排序(data,0,data.Length-1,Comparer.Default);
}
//案例2的第二种快速排序方法用于降低数组。
公共静态void快速排序(T[]数据,比较器)
{
快速排序(数据,0,数据长度-1,比较器);
}
//基于比较器的快速排序算法
公共静态void快速排序(T[]数组,左整数,右整数,比较器)
{
int i,j;
T轴,温度;
i=左;
j=右;
pivot=数组[(左+右)/2];
做
{
而((comparer.Compare(array[i],pivot)<0)和(ileft))j--;
如果(我创建一个类来保存所有这些字段,并使用适当的数据类型,如<代码> DATECTIME//CODE >来表示您的数据。@ IDLyPo介意我的数据在课堂上现在是什么?用您的新类更新您的帖子……然后我们将去那里。您应该考虑更改日期时间而不是单独的年、月、日和时间字段。相信我,谢谢。稍后再告诉我。既然你已经有了“地震CDATA”类,你想摆脱“MonthsArray”而是创建一个列表
来保存你的实例。你仍然会使用循环,但每次迭代你都会创建一个地震活动CDATA的动态实例,填充它的字段,然后将其添加到你的列表
。完成后,你可以使用LINQ或其他机制来过滤你想要的记录。你可以使用他将部分作为字符串组合到日期时间。由于您将月份作为单词,因此需要将区域性/提供者指定为。使用中的代码构建格式模式。
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 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);
}