C#有关排序算法
利用插入排序、希尔排序、快速排序等对随机生成的数字(如10个随机数)进行排序!这是我做的系统里的一部分,请问哪位高手能用其他算法再给个例子啊???谢谢谢谢了!!!
private void button1_Click(object sender, EventArgs e)
{
string s = "";
int i, j, temp;
for (i = 1; i < r.Length-1; i++)
{
for (j = 1; j < r.Length - i; j++)
if (r[j] > r[j + 1])
{
temp = r[j];
r[j] = r[j + 1];
r[j + 1] = temp;
}
//textBox2.Text= Convert.ToInt16(textBox1.Text).ToString();
if (int.Parse(ts.Text)>=i)
{
for (j = 1; j < r.Length;j++)
s = s + r[j] + " ";
s = s + "\r\n";
}
}
pxjg.Text = s;
} --------------------编程问答-------------------- http://topic.csdn.net/u/20090101/10/b1b76d48-3bf8-4b20-ae93-4ec7c8b4e75b.html --------------------编程问答-------------------- enum SortType
{
ASC,
DESC
}
class BubbleSort
{
private SortType order;
private int[] list;
private int iterateCount;
public SortType Order
{
get { return order; }
set { order = value; }
}
public int[] List
{
get { return list; }
set { list = value; }
}
public int IterateCount
{
get { return iterateCount; }
}
public BubbleSort()
{
order = SortType.ASC;
}
public BubbleSort(int[] arr)
{
list = arr;
order = SortType.ASC;
}
public BubbleSort(int[] arr, SortType type)
{
list = arr;
order = type;
}
private void Swap(ref int a, ref int b)
{
int c = a;
a = b;
b = c;
}
public void Sort()
{
bool isOK = false;
iterateCount = 0;
while (!isOK)
{
isOK = true;
for (int i = 0; i < list.Length - 1; i++)
{
iterateCount++;
switch (order)
{
case SortType.ASC:
{
if (list[i] > list[i + 1])
{
Swap(ref list[i], ref list[i + 1]);
isOK = false;
}
break;
}
case SortType.DESC:
{
if (list[i] < list[i + 1])
{
Swap(ref list[i], ref list[i + 1]);
isOK = false;
}
break;
}
}
}
}
}
public string GetDataString()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.Length; i++)
sb.Append(list[i]);
return sb.ToString();
}
public string GetDataString(string separator)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.Length; i++)
{
sb.Append(list[i]);
sb.Append(separator);
}
sb.Remove(sb.Length - separator.Length, separator.Length);
return sb.ToString();
}
}
http://topic.csdn.net/u/20090406/11/16f2af3e-32db-4d26-8345-e1ecf524553e.html --------------------编程问答--------------------
static void BubbleSort(int[] list)
{
int k,temp;
for (int i = 0; i < list.Length - 1; i++)
{
k = i;
for (int j = i + 1; j < list.Length; j++)
{
if (list[j] > list[k])
{
k = j;
}
}
if (k != i)
{
temp = list[i];
list[i] = list[k];
list[k] = temp;
}
}
}
补充:.NET技术 , C#