日期:2014-05-19  浏览次数:20818 次

排序问题???
请问怎样用C#实现一个排序哦??(如冒泡法哦)

------解决方案--------------------
using System;
public class SelectionSorter
{
// public enum comp {COMP_LESS,COMP_EQUAL,COMP_GRTR};
private int min;
// private int m=0;
public void Sort(int [] list)
{
for(int i=0;i <list.Length-1;++i)
{
min=i;
for(int j=i+1;j <list.Length;++j)
{
if(list[j] <list[min])
min=j;
}
int t=list[min];
list[min]=list[i];
list[i]=t;
// Console.WriteLine( "{0} ",list[i]);
}

}
}
public class MainClass
{
public static void Main()
{
int[] iArrary=new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
SelectionSorter ss=new SelectionSorter();
ss.Sort(iArrary);
for(int m=0;m <=13;m++)
Console.WriteLine( "{0} ",iArrary[m]);

}
}


------解决方案--------------------
using System;

public class SelectionSort
{
public int[] Sort(int[] list)
{
for (int i = 0; i < list.Length; i++)
{
for (int j = i+1; j < list.Length; j++)
{
if (list[i] < list[j])
{
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
return list;
}
}

public class Menu
{
static void Main()
{
int[] iArray = new int[] { 5, 9, 7, 3, 1, 5, 6, 8, 5, 2 };
int[] Result = new int[10];
SelectionSort ss = new SelectionSort();
Result = ss.Sort(iArray);
foreach (int i in Result)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}

------------------------------------------------
偶的答案,算法是一样,只是把类的返回不同~~
------解决方案--------------------
泛型版冒泡排序
class BubbleSort <T> where T:IComparable <T>
{
public static void Sort(T[] t)
{
for (int i = 0; i < t.Length; i++)
{
for (int j = 0; j < t.Length - i - 1; j++)
{
if (t[j].CompareTo(t[j + 1]) > 0)
{
T temp = t[j];
t[j] = t[j + 1];
t[j + 1] = temp;
}
}
}
}
}