冒泡,插入,选择,希尔排序
代码:
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
int[] array1 ={ 1, 3, 5, 7, 2, 4, 6, 8 };//初始化
Program ob = new Program();
ob.Sort4(array1);//排序
for (int i = 0; i < array1.Length; i++)//输出
{
Console.Write(array1[i] + ",");
}
Console.ReadLine();
}
private void Sort0(int[] list)//冒泡排序,(从后往前,小的排到最前面)
{
for (int i = 0; i < list.Length - 1; i++)
{
for (int j = list.Length - 1; j > i; j--)
{
if (list[j] < list[j - 1])
{
int temp = list[j];
list[j] = list[j - 1];
list[j - 1] = temp;
}
}
}
}
private void Sort1(int[] list)//冒泡排序,(从前往后,大的排到最后面)
{
int i, j, temp;
bool done = false;
j = 1;
while ((j < list.Length) && (!done))
{
done = true;
for (i = 0; i < list.Length - j; i++)
{
if (list[i] > list[i + 1])
{
done = false;
temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
j++;
}
}
private void Sort2(int[] list)//选择排序
{
int min;
for (int i = 0; i < list.Length - 1; i++)
{
min = i;
for (int j = i + 1; j < list.Length; j++)
{
if (list[j] < list[min])
min = j;
}
int t = list[min];
list[min] = list[i];
list[i] = t;
}
}
private void Sort3(int[] list)//插入排序
{
for (int i = 1; i < list.Length; i++)
{
int t = list[i];
int j = i;
while ((j > 0) && (list[j - 1] > t))
{
list[j] = list[j - 1];
--j;
}
list[j] = t;
}
}
private void Sort4(int[] list)//希尔排序
{
int inc;
for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
for (; inc > 0; inc /= 3)
{
for (int i = inc + 1; i <= list.Length; i += inc)
{
int t = list[i - 1];
int j = i;
while ((j > inc) && (list[j - inc - 1] > t))
{
list[j - 1] = list[j - inc - 1];
j -= inc;
}
list[j - 1] = t;
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
int[] array1 ={ 1, 3, 5, 7, 2, 4, 6, 8 };//初始化
Program ob = new Program();
ob.Sort4(array1);//排序
for (int i = 0; i < array1.Length; i++)//输出
{
Console.Write(array1[i] + ",");
}
Console.ReadLine();
}
private void Sort0(int[] list)//冒泡排序,(从后往前,小的排到最前面)
{
for (int i = 0; i < list.Length - 1; i++)
{
for (int j = list.Length - 1; j > i; j--)
{
if (list[j] < list[j - 1])
{
int temp = list[j];
list[j] = list[j - 1];
list[j - 1] = temp;
}
}
}
}
private void Sort1(int[] list)//冒泡排序,(从前往后,大的排到最后面)
{
int i, j, temp;
bool done = false;
j = 1;
while ((j < list.Length) && (!done))
{
done = true;
for (i = 0; i < list.Length - j; i++)
{
if (list[i] > list[i + 1])
{
done = false;
temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
j++;
}
}
private void Sort2(int[] list)//选择排序
{
int min;
for (int i = 0; i < list.Length - 1; i++)
{
min = i;
for (int j = i + 1; j < list.Length; j++)
{
if (list[j] < list[min])
min = j;
}
int t = list[min];
list[min] = list[i];
list[i] = t;
}
}
private void Sort3(int[] list)//插入排序
{
for (int i = 1; i < list.Length; i++)
{
int t = list[i];
int j = i;
while ((j > 0) && (list[j - 1] > t))
{
list[j] = list[j - 1];
--j;
}
list[j] = t;
}
}
private void Sort4(int[] list)//希尔排序
{
int inc;
for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
for (; inc > 0; inc /= 3)
{
for (int i = inc + 1; i <= list.Length; i += inc)
{
int t = list[i - 1];
int j = i;
while ((j > inc) && (list[j - inc - 1] > t))
{
list[j - 1] = list[j - inc - 1];
j -= inc;
}
list[j - 1] = t;
}
}
}
}
}