C# 排序方法

        static void InsertSort(int[] input)
{
if (input == null || input.Length == 0)
{
throw new Exception("input can't be empty.");
}

for (int i = 1; i < input.Length; i++)
{
if (input[i] < input[i - 1])
{
int tmp = input[i];
int j = i;
for (; j > 0 && tmp < input[j - 1]; j--)
{
input[j] = input[j - 1];
}
input[j] = tmp;
}
}
}

static void BubbleSort(int[] input)
{
if (input == null || input.Length == 0)
{
throw new Exception("input can't be empty.");
}

int temp = 0;
for (int i = 0; i < input.Length; i++)
{
for (int j = i + 1; j < input.Length; j++)
{
if (input[i] > input[j])
{
temp = input[i];
input[i] = input[j];
input[j] = temp;
}
}
}
}

static void SelectSort(int[] input)
{
int temp = 0;
int t = 0;
for (int i = 0; i < input.Length; i++)
{
t = i;
for (int j = i + 1; j < input.Length; j++)
{
if (input[t] > input[j])
{
t = j;
}
}
temp = input[t];
input[t] = input[i];
input[i] = temp;
}
}

static void QuickSort(int[] input, int left, int right)
{
int i = left - 1;
int j = right + 1;
int t = input[(i + j) / 2];
if (left < right)
{
while (true)
{
while (input[++i] < t) ;
while (input[--j] > t) ;
if (i >= j)
{
break;
}
Swap(ref input[i], ref input[j]);
}
QuickSort(input, left, i - 1);
QuickSort(input, j + 1, right);

}

}

static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
原文地址:https://www.cnblogs.com/Ligeance/p/2395961.html