这是我的 Gnome 排序算法,由于某种原因,当我使用它时,它会导致我的系统挂起。我认为这可能与该comparer.Compare(x,y) == 1
部分有关,但不确定。
static public void GnomeSort<T>(IList<T> list)
{
GnomeSort<T>(list, Comparer<T>.Default);
}
static public void GnomeSort<T>(IList<T> list, IComparer<T> comparer)
{
bool stillGoing = true;
while (stillGoing)
{
stillGoing = false;
for (int i = 1; i < list.Count; )
{
T temp;
T x = list[i - 1];
T y = list[i];
if (comparer.Compare(x,y) == 1)
i++;
else
{
temp = x;
x = y;
y = temp;
i--;
if (i == 0)
i = 1;
stillGoing = true;
}
}
}
}