问题描述
我有一个包含500万条记录的文件,其中包含数字
它们不按顺序排列(不规则)
您可以在下面找到文件结构:
I have a file with 5 million records which are include of numbers
that they are out of sequence (irregular)
you can find file structure below :
for instance desired Result
------------ ---------------
723,80 1,4
14,50 1,5
723,2 10,8
1,5 14,50
10,8 723,2
1,4 723,80
此结构显示不良状况和最佳状态我希望
达到最佳状态
最重要的(主要的)小费:
我没有使用任何技术,如linq,....
我想用可用的算法来安排文件。
此外(更多)应该考虑时间
所以,我们需要使用正确的算法将数字按顺序放入
一分钟内
谢谢
This structure displays bad condition and optimum condition and I
expect to reach the optimum
The most important (the main) tip :
I didn't use any techniques such as linq, ....
I want to do it with available algorithms and arrange the file.
furthermore (more over) the time should be considered
so, we need to use a proper algorithm to put the numbers in order
under a minute
Thanks
推荐答案
var lines = File.ReadAllLines(fileunordred);
int[] allCustomerIds = new int[lines.Length]; // make it the same length as lines
char[] splitter = new char[]{','};
for (int ix = 0; ix < ix.Length; ++ix)
{
var splitLine = lines[ix].Split(splitter, 2);
int customerId;
if (!int.TryParse(splitLine[0], out customerId)
{
// error parsing the data, do something "sensible"
allCustomerIds[ix] = -1; // some value to indicate a "bad" row, to sort together
}
allCustomerIds[ix] = customerId;
}
Array.Sort(allCustomerIds, lines);
// the <int,int> is wrong
// and <int,string> is unnecessary since the compiler can figure it out.
这两个数组现在按客户ID按数字升序排序。
只需使用 File.WriteAllLines(filename,lines)
制作已排序的文件。
大致与上面的非常类似,但现在首先比较第一个整数,然后是第二个整数组中具有相同第一个整数的整数。
both arrays are now sorted in ascending numerical order by the customer id.
just use File.WriteAllLines("filename", lines)
to make the sorted file.
Mostly very similar to the above, but now it compares first by the first integer and then by the second integer within the group that has the same first integer.
var lines = File.ReadAllLines(fileunordred);
int[] allInfo = new int[lines.Length]; // make it the same length as lines
char[] splitter = new char[]{','};
for (int ix = 0; ix < allInfo.Length; ++ix)
{
var splitLine = lines[ix].Split(splitter);
int[] pair= new int[2];
allInfo[ix] = pair;
int id;
if (!int.TryParse(splitLine[0], out id))
{
// error parsing the data, do something "sensible"
id = -1; // some value to indicate a "bad" row, to sort together
}
pair[0] = id;
if (!int.TryParse(splitLine[1], out id))
{
// error parsing the data, do something "sensible"
id = -1; // some value to indicate a "bad" row, to sort together
}
pair[1] = id;
}
Array.Sort(allInfo, (a, b) => {
int comp = a[0].CompareTo(b[0]);
return comp == 0 ? a[1].CompareTo(b[1]) : comp;
});
//Now just rewrite the file from the integers, (the lines array IS NOT sorted)
using (var out = new StreamWriter("outputfilename"))
{
foreach (var pair in allInfo)
{
out.WriteLine("{0},{1}", pair[0], pair[1]);
}
}
(我实际上没有试过这个,但它应该关闭......)
(I haven't actually tried this but it should be close...)
我没有使用任何技术,如linq,....
我想要使用可用的算法并安排文件。
I didn't use any tecniques such as linq, ....
I want to do it with available algorithms and arrange the file.
最好的排序算法之一是 []。快乐编码。
这篇关于500万条记录排序txt文件(不使用任何tecnique)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!