多种排序的汇总
一、首先总结一下C#中数组的排序
在C#中数组是System.Array的一个实例,对数组的排序可以通过Array的静态Sort方法。
eg1:
int[] intArray = new int[5] { 1, 2, 7, 4, 5 };
Array.Sort(intArray);
排序后intArray为{1,2,4,5,7}
eg2:
Point[] p = new Point[3];
p[0] = new Point(1, 2);
p[1] = new Point(2, 1);
p[2] = new Point(0, 3);
Array.Sort(p, delegate(Point p1, Point p2)
{
if (p1.X > p2.X)
return 1;
else if (p1.X == p2.X)
return 0;
else
return -1;
});
这里将调用Array的静态方法Sort<T>(T[] array, Comparison<T> comparison)进行排序;
排序后p为{(0,3),(1,2),(2,1)}
eg3:
Point[] p = new Point[3];
p[0] = new Point(1, 2);
p[1] = new Point(2, 1);
p[2] = new Point(0, 3);
Array.Sort(p, new ComparerPointByY());
ComparerPointByY比较器定义如下:
/// <summary>
/// 通过Point的Y坐标比较点的比较器
/// </summary>
public sealed class ComparerPointByY : IComparer
{
public ComparerPointByY() { }
#region IComparer 成员
public int Compare(object x, object y)
{
Point p1 = (Point)x; Point p2 = (Point)y;
if (p1.Y == p2.Y) return 0;
else{ return p1.Y > p2.Y ? 1 : -1;}
}
#endregion
}
这里将调用Array的静态方法Sort(Array array, IComparer comparer)进行排序;
排序后p为{(2,1),(1,2),(0,3)}
二、C#中集合的排序
三、常用的几种排序算法,总结如下
冒泡排序
public override void Sort(IList<T> list, IComparer<T> comparer)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
if (comparer == null)
{
throw new ArgumentNullException("comparer");
}
if (list.Count <= 1)
{
return;
}
for (int i = list.Count - 1; i >= 0; i--)
{
for (int j = 0; j < i; j++)
{
if (comparer.Compare(list[j], list[j + 1]) > 0)
{
Swap(list, j, j + 1);
}
}
}
}
protected void Swap(IList<T> list, int pos1, int pos2)
{
T tmp = list[pos1];
list[pos1] = list[pos2];
list[pos2] = tmp;
}
归并排序
public override void Sort(IList<T> list, IComparer<T> comparer)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
if (comparer == null)
{
throw new ArgumentNullException("comparer");
}
MergeSort(0, list.Count - 1, list, comparer);
}
private void MergeSort(int leftBoundary, int rightBoundary, IList<T> list, IComparer<T> comparer)
{
if (leftBoundary < rightBoundary)
{
int middle = (leftBoundary + rightBoundary) / 2;
MergeSort(leftBoundary, middle, list, comparer);
MergeSort(middle + 1, rightBoundary, list, comparer);
while ((middle + 1 <= rightBoundary) && (leftBoundary <= middle))
{
if (comparer.Compare(list[leftBoundary], list[middle + 1]) < 0)
{
leftBoundary++;
}
else
{
T currentItem = list[middle + 1];
for (int i = middle; i >= leftBoundary; i--)
{
list[i + 1] = list[i];
}
list[leftBoundary] = currentItem;
leftBoundary++;
middle++;
}
}
}
}
}
选择排序
public override void Sort(IList<T> list, IComparer<T> comparer)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
if (comparer == null)
{
throw new ArgumentNullException("comparer");
}
if (list.Count <= 1)
{
return;
}
int minIndex;
for (int i = 0; i < list.Count; i++)
{
minIndex = i;
for (int j = i + 1; j < list.Count; j++)
{
if (comparer.Compare(list[j], list[minIndex]) < 0)
{
minIndex = j;
}
}
Swap(list, i, minIndex);
}
}
持续更新中……
在C#中数组是System.Array的一个实例,对数组的排序可以通过Array的静态Sort方法。
eg1:
int[] intArray = new int[5] { 1, 2, 7, 4, 5 };
Array.Sort(intArray);
排序后intArray为{1,2,4,5,7}
eg2:
Point[] p = new Point[3];
p[0] = new Point(1, 2);
p[1] = new Point(2, 1);
p[2] = new Point(0, 3);
Array.Sort(p, delegate(Point p1, Point p2)
{
if (p1.X > p2.X)
return 1;
else if (p1.X == p2.X)
return 0;
else
return -1;
});
这里将调用Array的静态方法Sort<T>(T[] array, Comparison<T> comparison)进行排序;
排序后p为{(0,3),(1,2),(2,1)}
eg3:
Point[] p = new Point[3];
p[0] = new Point(1, 2);
p[1] = new Point(2, 1);
p[2] = new Point(0, 3);
Array.Sort(p, new ComparerPointByY());
ComparerPointByY比较器定义如下:
/// <summary>
/// 通过Point的Y坐标比较点的比较器
/// </summary>
public sealed class ComparerPointByY : IComparer
{
public ComparerPointByY() { }
#region IComparer 成员
public int Compare(object x, object y)
{
Point p1 = (Point)x; Point p2 = (Point)y;
if (p1.Y == p2.Y) return 0;
else{ return p1.Y > p2.Y ? 1 : -1;}
}
#endregion
}
这里将调用Array的静态方法Sort(Array array, IComparer comparer)进行排序;
排序后p为{(2,1),(1,2),(0,3)}
二、C#中集合的排序
三、常用的几种排序算法,总结如下
冒泡排序
public override void Sort(IList<T> list, IComparer<T> comparer)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
if (comparer == null)
{
throw new ArgumentNullException("comparer");
}
if (list.Count <= 1)
{
return;
}
for (int i = list.Count - 1; i >= 0; i--)
{
for (int j = 0; j < i; j++)
{
if (comparer.Compare(list[j], list[j + 1]) > 0)
{
Swap(list, j, j + 1);
}
}
}
}
protected void Swap(IList<T> list, int pos1, int pos2)
{
T tmp = list[pos1];
list[pos1] = list[pos2];
list[pos2] = tmp;
}
归并排序
public override void Sort(IList<T> list, IComparer<T> comparer)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
if (comparer == null)
{
throw new ArgumentNullException("comparer");
}
MergeSort(0, list.Count - 1, list, comparer);
}
private void MergeSort(int leftBoundary, int rightBoundary, IList<T> list, IComparer<T> comparer)
{
if (leftBoundary < rightBoundary)
{
int middle = (leftBoundary + rightBoundary) / 2;
MergeSort(leftBoundary, middle, list, comparer);
MergeSort(middle + 1, rightBoundary, list, comparer);
while ((middle + 1 <= rightBoundary) && (leftBoundary <= middle))
{
if (comparer.Compare(list[leftBoundary], list[middle + 1]) < 0)
{
leftBoundary++;
}
else
{
T currentItem = list[middle + 1];
for (int i = middle; i >= leftBoundary; i--)
{
list[i + 1] = list[i];
}
list[leftBoundary] = currentItem;
leftBoundary++;
middle++;
}
}
}
}
}
选择排序
public override void Sort(IList<T> list, IComparer<T> comparer)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
if (comparer == null)
{
throw new ArgumentNullException("comparer");
}
if (list.Count <= 1)
{
return;
}
int minIndex;
for (int i = 0; i < list.Count; i++)
{
minIndex = i;
for (int j = i + 1; j < list.Count; j++)
{
if (comparer.Compare(list[j], list[minIndex]) < 0)
{
minIndex = j;
}
}
Swap(list, i, minIndex);
}
}
持续更新中……