public static T[] RandomSort<T>(T[] array) { int len = array.Length; System.Collections.Generic.List<int> list = new System.Collections.Generic.List<int>(); T[] ret = new T[len]; Random rand = new Random(); int i = 0; while (list.Count < len) { int iter = rand.Next(0, len); if (!list.Contains(iter)) { list.Add(iter); ret[i] = array[iter]; i++; } } return ret; } 找着了。。结帖
对于仅仅是查询重复的,显然使用hash是比排序快更多的。我修改一下测试demo:using System; using System.Diagnostics; using System.Linq; using System.Text; using System.Collections.Generic;namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string[] alldit = CreateDatas(100000); var copy = new string[alldit.Length]; Array.Copy(alldit, copy, alldit.Length); test10(alldit); test1(copy); Console.ReadKey(); } private static void test10(string[] alldit) { Array.Sort(alldit); var reditnew = new StringBuilder(); Stopwatch watch = new Stopwatch(); watch.Start(); string last = null; foreach (string tmpdit in alldit) { if (last == null || tmpdit != last) { reditnew.AppendLine(tmpdit); last = tmpdit; } } string result = reditnew.ToString(); watch.Stop(); Console.WriteLine("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n"); } private static string[] CreateDatas(int p) //随机产生p和字符串 { var Rnd = new Random(); return (from n in Enumerable.Range(1, p) select Rnd.Next().ToString()).ToArray(); } private static void test1(string[] alldit) { var reditnew = new StringBuilder(); Stopwatch watch = new Stopwatch(); watch.Start(); Dictionary<string, int> dict = new Dictionary<string, int>(); foreach (string tmpdit in alldit) { if (dict.ContainsKey(tmpdit)) { reditnew.AppendLine(tmpdit); } } var result = reditnew.ToString(); watch.Stop(); Console.WriteLine("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n"); } } } 后边那个新的测试使用的是hash字典。而且它也是按照原来文件中的次序排的,无需“打乱”次序。
Stopwatch watch = new Stopwatch();
watch.Start(); string[] alldit= File.ReadAllLines("d:\\alldit.txt"); foreach(string tmpdit in alldit)
{
if (!reditnew.Contains(tmpdit))
{
reditnew +=tmpdit;
}
}
watch.Stop();
textview.Text += ("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
File.WriteAllText("d:\\newdit.txt", reditnew);
Console.Beep();
这是我现在的代码一个小时了也不见结束
private void button1_Click(object sender, EventArgs e)
{
string reditnew = "";
Stopwatch watch = new Stopwatch();
watch.Start(); string[] alldit = File.ReadAllLines("d:\\alldit.txt"); foreach (string tmpdit in alldit)
{
if (!reditnew.Contains(tmpdit))
{
reditnew += tmpdit + "\r\n";
}
}
watch.Stop();
textview.Text += ("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
File.WriteAllText("d:\\newdit.txt", reditnew);
Console.Beep();
}
这是我现在的代码,更正二楼的漏了换行
文本文件发过来
private void button1_Click(object sender, EventArgs e)
{
Stopwatch watch = new Stopwatch();
watch.Start();
string reditnew = "";
string[] alldit = File.ReadAllLines("d:\\alldit.txt");
Array.Sort(alldit); for (int i = 0; i < alldit.Length - 1; i++)
{
if (alldit[i].Split('$')[0] == alldit[i + 1].Split('$')[0])
{ alldit[i] = ""; }
else
{
if (alldit[i].Length > 1)
{
reditnew += alldit[i] + "\r\n";
}
}
}
reditnew += alldit[alldit.Length - 1] + "\r\n";
watch.Stop();
textview.Text += ("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
File.WriteAllText("d:\\newdit.txt", reditnew);
Console.Beep();新代码,请帮忙改进
private void button1_Click(object sender, EventArgs e)
{
Stopwatch watch = new Stopwatch();
watch.Start();
string reditnew = "";
string[] alldit = File.ReadAllLines("d:\\alldit.txt");
Array.Sort(alldit); for (int i = 0; i < alldit.Length - 1; i++)
{
if (alldit[i].Split('$')[0] == alldit[i + 1].Split('$')[0])
{ alldit[i] = ""; }
else
{
if (alldit[i].Length > 1)
{
reditnew += alldit[i] + "\r\n";
}
}
}
reditnew += alldit[alldit.Length - 1] + "\r\n";
watch.Stop();
textview.Text += ("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
File.WriteAllText("d:\\newdit.txt", reditnew);
Console.Beep();
}
这个再帮忙再改进下效率也不知道怎么再随机打乱排序
using System.Diagnostics;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] alldit = CreateDatas(100000);
var copy = new string[alldit.Length];
Array.Copy(alldit, copy, alldit.Length);
test10(alldit);
test1(copy);
Console.ReadKey();
} private static void test10(string[] alldit)
{
Array.Sort(alldit);
var reditnew = new StringBuilder();
Stopwatch watch = new Stopwatch();
watch.Start();
string last = null;
foreach (string tmpdit in alldit)
{
if (last == null || tmpdit != last)
{
reditnew.AppendLine(tmpdit);
last = tmpdit;
}
}
string result = reditnew.ToString();
watch.Stop();
Console.WriteLine("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
} private static string[] CreateDatas(int p) //随机产生p和字符串
{
var Rnd = new Random();
return (from n in Enumerable.Range(1, p)
select Rnd.Next().ToString()).ToArray();
} private static void test1(string[] alldit)
{
string reditnew = "";
Stopwatch watch = new Stopwatch();
watch.Start();
foreach (string tmpdit in alldit)
{
if (!reditnew.Contains(tmpdit))
{
reditnew += tmpdit + "\r\n";
}
}
watch.Stop();
Console.WriteLine("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Stopwatch watch = new Stopwatch();
watch.Start();
string reditnew = "";
StringBuilder newalldit = new StringBuilder();
string[] alldit = File.ReadAllLines("d:\\alldit.txt");
Array.Sort(alldit); for (int i = 0; i < alldit.Length - 1; i++)
{
if (alldit[i].Split('$')[0] == alldit[i + 1].Split('$')[0])
{ alldit[i] = ""; }
else
{
if (alldit[i].Length > 1)
{
// reditnew += alldit[i] + "\r\n";
newalldit.AppendLine(alldit[i]);
}
}
}
//reditnew += alldit[alldit.Length - 1] + "\r\n";
newalldit.AppendLine(alldit[alldit.Length - 1]);
watch.Stop();
textview.Text += ("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
reditnew = newalldit.ToString();
File.WriteAllText("d:\\newdit.txt", reditnew);
Console.Beep();
}
再次改进。性能OK了。。麻烦帮忙教我怎么打乱排序。谢谢
请教我下现在怎么打乱他的排序
public static T[] RandomSort<T>(T[] array)
{
int len = array.Length;
System.Collections.Generic.List<int> list = new System.Collections.Generic.List<int>();
T[] ret = new T[len];
Random rand = new Random();
int i = 0;
while (list.Count < len)
{
int iter = rand.Next(0, len);
if (!list.Contains(iter))
{
list.Add(iter);
ret[i] = array[iter];
i++;
} }
return ret;
}
找着了。。结帖
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Collections.Generic;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] alldit = CreateDatas(100000);
var copy = new string[alldit.Length];
Array.Copy(alldit, copy, alldit.Length);
test10(alldit);
test1(copy);
Console.ReadKey();
} private static void test10(string[] alldit)
{
Array.Sort(alldit);
var reditnew = new StringBuilder();
Stopwatch watch = new Stopwatch();
watch.Start();
string last = null;
foreach (string tmpdit in alldit)
{
if (last == null || tmpdit != last)
{
reditnew.AppendLine(tmpdit);
last = tmpdit;
}
}
string result = reditnew.ToString();
watch.Stop();
Console.WriteLine("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
} private static string[] CreateDatas(int p) //随机产生p和字符串
{
var Rnd = new Random();
return (from n in Enumerable.Range(1, p)
select Rnd.Next().ToString()).ToArray();
} private static void test1(string[] alldit)
{
var reditnew = new StringBuilder();
Stopwatch watch = new Stopwatch();
watch.Start();
Dictionary<string, int> dict = new Dictionary<string, int>();
foreach (string tmpdit in alldit)
{
if (dict.ContainsKey(tmpdit))
{
reditnew.AppendLine(tmpdit);
}
}
var result = reditnew.ToString();
watch.Stop();
Console.WriteLine("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
}
}
}
后边那个新的测试使用的是hash字典。而且它也是按照原来文件中的次序排的,无需“打乱”次序。