几种排序算法
//冒泡排序法
public void Sort(int[] source)
{
Console.WriteLine("冒泡算法开始......");
Auxiliary.DisplayArray(source);
int j, temp;
j = 1;
while ((j < source.Length))
{
for (int i = 0; i < source.Length - j; i++)
{
if (source[i] < source[i + 1])
{
temp = source[i];
source[i] = source[i + 1];
source[i + 1] = temp;
}
}
//希尔排序法
public void Sort(int[] source)
{
int inc;
for (inc = 1; inc <= source.Length / 9; inc = 3 * inc + 1) ;
for (; inc > 0; inc /= 3)
{
for (int i = inc + 1; i <= source.Length; i += inc)
{
int t = source[i - 1]; int j = i;
while ((j > inc) && (source[j - inc - 1] > t))
{
source[j - 1] = source[j - inc - 1]; j -= inc;
}
source[j - 1] = t;
}
}
//插入排序算法
public void Sort(int[] source)
{
for (int i = 1; i < source.Length; i++)
{
int t = source[i];
int j = i;
while ((j > 0) && (source[j - 1] < t))
{
source[j] = source[j - 1];
--j;
}
source[j] = t;
}
}
//选择排序算法
public void Sort(int[] source)
{
int min;
int count = 1;
Console.WriteLine("选择算法开始......");
Auxiliary.DisplayArray(source);
for (int i = 0; i < source.Length - 1; i++)
{
min = i;
for (int j = i + 1; j < source.Length; j++)
{
if (source[j] > source[min])
min = j;
}
int t = source[min];
source[min] = source[i];
source[i] = t;
Console.Write("第{0}趟排序:", count);
Auxiliary.DisplayArray(source);
count++;
}
}