ListBox里面有一些String 下面是我的算法private void RandomPut()
{
Random rd = new Random();
string Obj;
progressBar.MaxNum=listBox.Items.Count;
progressBar.value=0;
for(int n=0;n<listBox.Items.Count;n++)
{
Obj = listBox.Items[n].ToString();
listBox.Items.RemoveAt(n);
listBox.Items.Insert(rd.Next(listBox.Items.Count), Obj);
progressBar.PerformStep();
}
}当listBox.Items.Count<20000 的时候 尚可接受
但是如果listBox.Items.Count>100000 进行的速度足以让任何人抓狂...
求高人指点 如何更快速的乱序
{
Random rd = new Random();
string Obj;
progressBar.MaxNum=listBox.Items.Count;
progressBar.value=0;
for(int n=0;n<listBox.Items.Count;n++)
{
Obj = listBox.Items[n].ToString();
listBox.Items.RemoveAt(n);
listBox.Items.Insert(rd.Next(listBox.Items.Count), Obj);
progressBar.PerformStep();
}
}当listBox.Items.Count<20000 的时候 尚可接受
但是如果listBox.Items.Count>100000 进行的速度足以让任何人抓狂...
求高人指点 如何更快速的乱序
解决方案 »
- 怎样在winform插入代码
- BinarySearch(Array array, Object value) 总返回负数
- C# ftp 主动模式
- winform里上传多张图片
- 在VS2005中写C#代码时,建立第一个类后可以编译,为什么建立第二个类的时候还是编译第一个类呢?
- 我的第一次
- 怎样打印web页面中的panel控件中的内容?(100分求救)
- 我的电脑经常在看网页和压缩文件的时候死机,请问是cpu问题吗?
- VS.NET的.dll组件生成后,如何配置成DCOM组件。供远程调用。
- ODBC连接问题
- .net自带了一套系统图标的,比如我的电脑图标,硬盘图标之类的
- 求C#个一个简单的打字游戏代码??
2. In terms of data structure, you might change insert to swap (you need to write it yourself)
private void RandomPut()
{
Random rd = new Random();
string Obj;
progressBar.MaxNum=listBox.Items.Count;
progressBar.value=0; listBox.BeginUpdate(); //<---
for(int n=0;n<listBox.Items.Count;n++)
{
Obj = listBox.Items[n].ToString();
listBox.Items.RemoveAt(n);
listBox.Items.Insert(rd.Next(listBox.Items.Count), Obj);
progressBar.PerformStep();
}
listBox.EndUpdate(); //<---
}
最后结果有的数字没有被移位,有的数字被随机移位N次。
再说了你的代码包含RemoveAt的删除方法,好像比较好的习惯应该是循环从最高位到最低位吧,你的循环变量从0开始走高,好像有潜在的bug危险,虽然你的现在这个程序估计没问题。
button1是加入数据,button2是乱序排列using System;
using System.Collections.Generic;
using System.Windows.Forms;namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} delegate void dele(); private void button1_Click(object sender, EventArgs e)
{
listBox1.BeginUpdate();
for(int i = 0;i < 100000;i++)
listBox1.Items.Add(i);
listBox1.EndUpdate();
} private void button2_Click(object sender, EventArgs e)
{
List<object> list = new List<object>();
foreach(object item in listBox1.Items)
list.Add(item);
list.Sort(new Cmp());
listBox1.BeginUpdate();
listBox1.Items.Clear();
foreach(object item in list)
listBox1.Items.Add(item);
listBox1.EndUpdate();
}
}
class Cmp : IComparer<object>
{
Random rnd = new Random();
#region IComparer<object> 成员 public int Compare(object x, object y)
{
return rnd.Next(3) - 1;
} #endregion
}
}
返回 -1 0 1 分别表示 x<y x=y x>y 吧明天在Form上试试这个效率怎样
先声明全局变量 Cmp cmp = new Cmp();
再用
list.Sort(cmp);
我就是这样做的 但是问题依旧...
事实上 是这样做的Cmp cp = new Cmp();
foreach (string item in listBox.Items)
{
list.Add(item);
progressBar1.PerformStep();
}
try
{
list.Sort(cp);
}
catch
{
}加了try catch以后 不处理这个异常 也就是只进行了部分排序
{
static Random rnd = new Random();
#region IComparer<object> 成员 public int Compare(T x, T y)
{
if (x.Equals(y))
return 0;
return rnd.Next(3) - 1;
} #endregion
}