代码拉取完成,页面将自动刷新
using System.Collections.Generic;
using DataStructures.Lists;
namespace Algorithms.Sorting
{
/// <summary>
/// Implements this Insertion Sort algorithm over ArrayLists.
/// </summary>
public static class InsertionSorter
{
//
// The quick insertion sort algorithm.
// For any collection that implements the IList interface.
public static void InsertionSort<T>(this IList<T> list, Comparer<T> comparer = null)
{
//
// If the comparer is Null, then initialize it using a default typed comparer
comparer = comparer ?? Comparer<T>.Default;
// Do sorting if list is not empty.
int i, j;
for (i = 1; i < list.Count; i++)
{
T value = list[i];
j = i - 1;
while ((j >= 0) && (comparer.Compare(list[j], value) > 0))
{
list[j + 1] = list[j];
j--;
}
list[j + 1] = value;
}
}
//
// The quick insertion sort algorithm.
// For the internal ArrayList<T>. Check the DataStructures.ArrayList.cs.
public static void InsertionSort<T>(this ArrayList<T> list, Comparer<T> comparer = null)
{
//
// If the comparer is Null, then initialize it using a default typed comparer
comparer = comparer ?? Comparer<T>.Default;
for (int i = 1; i < list.Count; i++)
{
for (int j = i; j > 0; j--)
{
if (comparer.Compare(list[j], list[j - 1]) < 0) //(j)th is less than (j-1)th
{
var temp = list[j - 1];
list[j - 1] = list[j];
list[j] = temp;
}
}
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。