//冒泡排序算法
static void Main(string[] args)
{
int[] array = { 2, 3, 7, 1, 4, 9, 6, 8, 10 };
var newarrry = BubbleSort(array);
for (int i = 0; i < newarrry.Length; i++)
{
Console.WriteLine(newarrry[i]);
}
Console.Read();
}
public static int[] BubbleSort(int[] array)
{
int temp = 0;
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = i + 1; j < array.Length; j++)
{
//if (array[j] < array[i])//冒泡升序
//{
// temp = array[i];
// array[i] = array[j];
// array[j] = temp;
//}
if (array[j] > array[i])//冒泡降序
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return array;
}
//选择排序算法
static void Main(string[] args)
{
int[] array = { 2, 3, 7, 1, 4, 9, 6, 8, 10 };
var newarrry = SelectSort(array);
for (int i = 0; i < newarrry.Length; i++)
{
Console.WriteLine(newarrry[i]);
}
Console.Read();
}
public static int[] SelectSort(int[] array)
{
int temp = 0;
int minindex = 0;
for (int i = 0; i < array.Length - 1; i++)
{
minindex = i;
for (int j = i; j < array.Length; j++)
{
if (array[j] < array[minindex])//每次循环选择最小的数插入到数组中
{
minindex = j;
}
}
temp = array[minindex];
array[minindex] = array[i];
array[i] = temp;
}
return array;
}
//插入排序算法
static void Main(string[] args)
{
int[] array = { 2, 3, 7, 1, 4, 9, 6, 8, 10 };
var newarrry = InsertSort(array);
for (int i = 0; i < newarrry.Length; i++)
{
Console.WriteLine(newarrry[i]);
}
Console.Read();
}
public static int[] InsertSort(int[] array)
{
for (int i = 1; i < array.Length; i++)
{
int t = array[i];
int j = i;
while ((j > 0) && (array[j - 1] > t))
{
array[j] = array[j - 1];
j--;
}
array[j] = t;
}
return array;
}
//希尔排序算法
static void Main(string[] args)
{
int[] array = { 2, 3, 7, 1, 4, 9, 6, 8, 10 };
var newarrry = ShellSort(array);
for (int i = 0; i < newarrry.Length; i++)
{
Console.WriteLine(newarrry[i]);
}
Console.Read();
}
public static int[] ShellSort(int[] array)
{
int inc;
for (inc = 1; inc <= array.Length / 9; inc = 3 * inc + 1) ;
for (; inc > 0; inc /= 3)
{
for (int i = inc + 1; i <= array.Length; i += inc)
{
int t = array[i - 1];
int j = i;
while ((j > inc) && (array[j - inc - 1] > t))
{
array[j - 1] = array[j - inc - 1];
j -= inc;
}
array[j - 1] = t;
}
}
return array;
}