目录
主类:
class Program
{
static void Main(string[] args)
{
int[] arr = new int[10];
Program p = new Program();
Console.WriteLine("这里有一个数组arr,以下展示 8 种排序算法\n");
Console.WriteLine("原数组内容是 : 9 8 7 6 5 4 3 2 1 0\n");
p.Print__();p.Reserve(arr);
//冒泡排序
Bubble_Sort bs = new Bubble_Sort();
bs.Bubble_sort(arr);
p.Print__();p.Reserve(arr);
//鸡尾酒排序(冒泡排序改良)
Bubble_Sort_plus bsp = new Bubble_Sort_plus();
bsp.Bubble_sort_plus(arr);
p.Print__();p.Reserve(arr);
//快速排序
Quick_Sort qs = new Quick_Sort();
Console.WriteLine("快速排序:(数据越不规整,排的就越快)\n");
qs.Quick_sort(arr, 0, arr.Length - 1);
p.Print__();p.Reserve(arr);
//选择排序
Select_Sort ss = new Select_Sort();
ss.Select_sort(arr);
p.Print__();p.Reserve(arr);
//插入排序
Insert_Sort Is = new Insert_Sort();
Is.Insert_sort(arr);
p.Print__();p.Reserve(arr);
//希尔排序
Shell_Sort Ss = new Shell_Sort();
Ss.Shell_sort(arr);
p.Print__();p.Reserve(arr);
//桶排序
Bucket_Sort bss = new Bucket_Sort();
bss.Bucket_sort(arr);
p.Print__();p.Reserve(arr);
//归并排序
Merge_Sort_InOrder msi = new Merge_Sort_InOrder();
Console.WriteLine("归并排序 : \n");
msi.MergeSort(arr);
p.Print__();p.Reserve(arr);
Console.ReadKey();
}
//重置arr
public void Reserve(int[] A)
{
for(int i = 0; i < 10; i++)
A[i] = 9 - i;
}
private void Print__()
{
Console.WriteLine("----------------------------------------------------------\n");
}
public void Print_Ans(int[] A)
{
foreach (int x in A)
Console.Write(x + " ");
Console.WriteLine("\n");
}
public void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
}
冒泡排序:
//冒泡排序 时间复杂度: O ( N^2 ) 稳定
class Bubble_Sort
{
public void Bubble_sort(int[] arr)
{
Console.WriteLine("冒泡排序 : \n");
int len = arr.Length;
Program p = new Program();
for (int i = 0; i < len - 1; i++)
{
for(int j = 0; j < len - i - 1; j++)
if (arr[j] > arr[j + 1])
p.Swap(ref arr[j], ref arr[j + 1]);
Console.Write("第" + (i + 1) + "轮排序后 : ");
p.Print_Ans(arr);
}
}
}
鸡尾酒排序:
class Bubble_Sort_plus
{
public void Bubble_sort_plus(int[] arr)
{
Console.WriteLine("改良冒泡排序:\n");
Program p = new Program();
int len = arr.Length;
int left = 0, right = len - 1;
int k = 0;
while (left < right)
{
for (int i = left + 1; i <= right; i++)
if (arr[left] > arr[i])
p.Swap(ref arr[left], ref arr[i]);
left++;
for (int i = right - 1; i >= left; i--)
if (arr[right] < arr[i])
p.Swap(ref arr[right], ref arr[i]);
right--;
Console.Write("第" + (++k) + "轮排序后 : ");
p.Print_Ans(arr);
}
}
}
快速排序:
class Quick_Sort
{
Program p = new Program();
int k = 0;
public void Quick_sort(int[] arr, int l, int r)
{
if (l >= r) return;
int i, j, x;
i = l;j = r;x = arr[i];
while (i < j)
{
while (i < j && arr[j] > x)
j--;
if (i < j) arr[i++] = arr[j];
while (i < j && arr[i] < x)
i++;
if (i < j) arr[j--] = arr[i];
}
arr[i] = x;
Console.Write("第" + (++k) + "轮排序后 : ");
p.Print_Ans(arr);
Quick_sort(arr, l, i - 1);
Quick_sort(arr, i + 1, r);
}
}
选择排序:
//选择排序 时间复杂度 : O ( N^2 ) 不稳定
class Select_Sort
{
public void Select_sort(int[] arr)
{
Console.WriteLine("选择排序:\n");
Program p = new Program();
int k = 0;
int len = arr.Length;
for(int i = 0; i < len; i++)
{
int min = i;
for (int j = i + 1; j < len; j++)
if (arr[j] < arr[min])
min = j;
if (min != i)
{
p.Swap(ref arr[min], ref arr[i]);
Console.Write("第" + (++k) + "轮排序后 : ");
p.Print_Ans(arr);
}
}
}
}
插入排序:
//插入排序 时间复杂度 : O ( N^2 ) 稳定
class Insert_Sort
{
Program p = new Program();
public void Insert_sort(int[] arr)
{
int kk = 0;
Console.WriteLine("插入排序 : \n\n");
int len = arr.Length;
for(int i = 1; i < len; i++)
{
int j, k;
for (j = i - 1; j >= 0; j--)
if (arr[j] < arr[i])
break;
if (j != i - 1)
{
int tmp = arr[i];
for (k = i - 1; k > j; k--)
arr[k + 1] = arr[k];
arr[k + 1] = tmp;
Console.Write("第" + (++kk) + "轮排序后 : ");
p.Print_Ans(arr);
}
}
}
}
希尔排序:
//希尔排序 时间复杂度 : O ( N^(3/2) ) 稳定
class Shell_Sort
{
Program p = new Program();
public void Shell_sort(int[] arr)
{
Console.WriteLine("希尔排序 : \n");
int len = arr.Length;
int gap, kk = 0;
for (gap = len / 2; gap > 0; gap /= 2)
{
int i, j;
for (i = 0; i < gap; i++)
{
for (j = i + gap; j < len; j+=gap)
{
if (arr[j] < arr[j - gap])
{
int tmp = arr[j];
int k = j - gap;
while (k >= 0 && arr[k] > tmp)
{
arr[k + gap] = arr[k];
k -= gap;
}
arr[k + gap] = tmp;
}
}
}
Console.Write("第" + (++kk) + "轮排序后 : ");
p.Print_Ans(arr);
}
}
}
桶排序:
//桶排序 时间复杂度 : O ( x * N ) 稳定
class Bucket_Sort
{
public void Bucket_sort(int[] arr)
{
//Program p = new Program();
Console.WriteLine("桶排序(空间换时间):\n");
int[] book = new int[1005];
int maxn = -(1<<30);
foreach(int x in arr)
{
book[x]++;
maxn = x > maxn ? x : maxn;
}
Console.Write("第1轮排序后 : ");
for (int i = 0; i <= maxn; i++)
if (book[i]>=1)
Console.Write(i + " ");
Console.WriteLine("\n");
}
}
归并排序:
//归并排序 时间复杂度 : O ( N logN ) 稳定
class Merge_Sort_InOrder
{
Program p = new Program();
public static int kk = 0;
public void MergeSort(int[] array)
{
MergeSort(array, 0, array.Length - 1);
}
private static void MergeSort(int[] array, int left, int right)
{
if (left < right)
{
int mid = (left + right) / 2;
MergeSort(array, left, mid);
MergeSort(array, mid + 1, right);
Merge(array, left, mid, right);
}
}
private static void Merge(int[] arr, int p, int q, int r)
{
int[] L = new int[q - p + 2];
int[] R = new int[r - q + 1];
L[q - p + 1] = int.MaxValue;
R[r - q] = int.MaxValue;
for (int i = 0; i < q - p + 1; i++)
{
L[i] = arr[p + i];
}
for (int i = 0; i < r - q; i++)
{
R[i] = arr[q + 1 + i];
}
int j = 0;
int k = 0;
for (int i = 0; i < r - p + 1; i++)
{
if (L[j] <= R[k])
{
arr[p + i] = L[j];
j++;
}
else
{
arr[p + i] = R[k];
k++;
}
}
Console.Write("第" + (++kk) + "轮排序后 : ");
foreach (int x in arr)
Console.Write(x + " ");
Console.WriteLine("\n");
}
}
转载:https://blog.csdn.net/lesileqin/article/details/101470342
查看评论