我基本上处理的是一个项目,我必须在一些文件中读取一个包含大量值的文件,其中每行属于1个项目。我已经设法将每个文件读入一个字符串数组,然后我使用锯齿状数组将每个项目的信息放入行中。我现在遇到的问题是,该项目要求我允许用户选择他们希望按项目列表排序的值集(快速排序等)。我能够实现一个快速排序算法,但我发现很难(通过大量的研究)找出我在排序算法中如何将所有值保持在一起。 例如: 如果用户选择按年份排序,我的程序将对年份进行排序,但如果我要将新的排序列表保存到锯齿状数组中,则其他信息(时间,日期,纬度等)将不对应于同一项目
这是我到目前为止的代码:
static void Main(string[] args)
{
var lineCount = File.ReadLines(@"C:\path\Year_1.txt").Count();
List[] Items = new List[lineCount];
string[] year = File.ReadAllLines(@"C:\path\Year_1.txt");
string[] month = File.ReadAllLines(@"C:\path\Month_1.txt");
string[] day = File.ReadAllLines(@"C:\path\Day_1.txt");
string[] time = File.ReadAllLines(@"C:\path\Time_1.txt");
string[] depth = File.ReadAllLines(@"C:\path\Depth_1.txt");
string[] timestamp = File.ReadAllLines(@"C:\path\Timestamp_1.txt");
string[] region = File.ReadAllLines(@"C:\path\Region_1.txt");
string[] iris_ID = File.ReadAllLines(@"C:\path\IRIS_ID_1.txt");
string[] latitude = File.ReadAllLines(@"C:\path\Latitude_1.txt");
string[] longitude = File.ReadAllLines(@"C:\path\Longitude_1.txt");
StreamReader file = new StreamReader(@"C:\path\Year_1.txt");
string line = file.ReadLine();
string[][] Array = new string[lineCount][];
for (int i = 0; i < lineCount; i++)
for(int j = 0; j < 10; j++)
{
if (j < 10)
{
Array[i] = new string[10] { year[i], month[i], day[i], time[i], depth[i], timestamp[i], region[i], iris_ID[i], latitude[i], longitude[i] };
Console.WriteLine("Year:\tMonth:\tDay:\tTime:\tDepth:\tTimestamp:\tRegion:\tIris_ID:\tLatitude:\tLongitude:\n{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}", Array[i][0], Array[i][1], Array[i][2], Array[i][3], Array[i][4], Array[i][5], Array[i][6], Array[i][7], Array[i][8], Array[i][9]);
}
else { j = 0; }
}
Console.WriteLine("Please select the option you wish to sort the collection of data by (1 - 10)");
Console.WriteLine(" 1. Year\n 2. Month\n 3. Day\n 4. Time\n 5. Depth\n 6. Timestamp\n 7. Region\n 8. Iris_ID\n 9. Latitude\n 10. Longitude");
int Choice = Convert.ToInt32(Console.ReadLine());
if (Choice == 1)
{
Quicksort(year, 0, lineCount - 1);
// Print the sorted array
for (int i = 0; i < lineCount; i++)
{
Console.Write(year[i] + ",\n");
}
}
public static void Quicksort(IComparable[] elements, int left, int right)
{
int i = left, j = right;
IComparable pivot = elements[(left + right) / 2];
while (i <= j)
{
while (elements[i].CompareTo(pivot) < 0)
{
i++;
}
while (elements[j].CompareTo(pivot) > 0)
{
j--;
}
if (i <= j)
{
// Swap
IComparable tmp = elements[i];
elements[i] = elements[j];
elements[j] = tmp;
i++;
j--;
}
}
// Recursive calls
if (left < j)
{
Quicksort(elements, left, j);
}
if (i < right)
{
Quicksort(elements, i, right);
}
}