您的位置:首页 > 其它

几种常见的排序算法

2017-03-14 14:25 141 查看
交换两个对象(排序中会用到)

private static void Interop_ExchangeValue<T>(ref T m, ref T n)
{
T Temp = m;
m = n;
n = Temp;
}


插入排序

public static void InsertSort<T>(ref T[] array, IComparer<T> comparer)
{
if (array.Length > 0)
{
for (int i = 1; i < array.Length; i++)
{
int front = i - 1;
int index = i;
while (front >= 0)
{
if (comparer.Compare(array[index], array[front]) < 0)
{
Interop_ExchangeValue(ref array[index], ref array[front]);
index = front;
}
front--;
}
}
}
}


选择排序

public static void SelectSort<T>(ref T[] array, IComparer<T> comparer)
{
if (array.Length > 0)
{
for (int i = 0; i < array.Length; i++)
{
T minValue = array[i];
int minIndex = i;
for (int j = i; j < array.Length; j++)
{
if (comparer.Compare(minValue, array[j]) > 0)
{
minValue = array[j];
minIndex = j;
}
}
Interop_ExchangeValue(ref array[i], ref array[minIndex]);
}
}
}


冒泡排序

public static void BubbleSort<T>(ref T[] array, IComparer<T> comparer)
{
if (array.Length > 0)
{
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = array.Length - 1; j > i; j--)
{
if (comparer.Compare(array[j - 1], array[j]) > 0)
{
Interop_ExchangeValue(ref array[j - 1], ref array[j]);
}
}
}
}
}


双向冒泡排序

public static void BidirectionalBubleSort<T>(ref T[] array, IComparer<T> comparer)
{
if (array.Length > 0)
{
int end = array.Length;
int start = -1;
bool swapped = false;
do
{
swapped = false;
start++;
end--;

for (int j = start; j < end; j++)
{
if (comparer.Compare(array[j], array[j + 1]) > 0)
{
Interop_ExchangeValue(ref array[j], ref array[j + 1]);
swapped = true;
}
}
for (int j = end - 1; j >= start; j--)
{
if (comparer.Compare(array[j], array[j + 1]) > 0)
{
Interop_ExchangeValue(ref array[j], ref array[j + 1]);
swapped = true;
}
}
} while (start < end && swapped);
}
}


快速排序

public static void QuickSort<T>(ref T[] array, IComparer<T> comparer)
{
if (array.Length > 0)
{
Interop_QuickSort<T>(ref array, 0, array.Length - 1, comparer);
}
}

private static void Interop_QuickSort<T>(ref T[] array, int left, int right, IComparer<T> comparer)
{
if (left < right)
{
T key = array[(left + right) / 2];
int i = left - 1;
int j = right + 1;
while (true)
{
while (comparer.Compare(array[++i], key) < 0) ;
while (comparer.Compare(array[--j], key) > 0) ;
if (i >= j)
{
break;
}
Interop_ExchangeValue(ref array[i], ref array[j]);
}
Interop_QuickSort(ref array, left, i - 1, comparer);
Interop_QuickSort(ref array, j + 1, right, comparer);
}
}


堆排序

public static void HeapSort<T>(ref T[] array, IComparer<T> comparer)
{
for (int i = (array.Length - 1) / 2; i >= 0; i--)
{
Interop_HeapAdjust(ref array, i, array.Length - 1, comparer);
}
for (int i = array.Length - 1; i >= 1; i--)
{
Interop_ExchangeValue<T>(ref array[0], ref array[i]);
Interop_HeapAdjust(ref array, 0, i - 1, comparer);
}
}

private static void Interop_HeapAdjust<T>(ref T[] array, int index, int length, IComparer<T> comparer)
{
T Temp = array[index];
int j = index * 2 + 1;
while (j <= length)
{
if (j < length)
{
if (comparer.Compare(array[j], array[j + 1]) < 0)
{
j = j + 1;
}
}
if (comparer.Compare(Temp, array[j]) < 0)
{
array[index] = array[j];
index = j;
j = 2 * index + 1;
}
else
{
j = length + 1;
}
}
array[index] = Temp;
}


在排序的时候,需要实现IComparer接口,可以用在合适的地方。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  排序算法