排序算法——C#语言

插入排序:(直接插入排序&希尔排序)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Sort

{

public class InsertSort

{

public static int k;

 

/// <summary>

/// 直接插入排序

/// </summary>

/// <param name="a">数组</param>

/// <param name="n">数组的长度</param>

public static void DirectInsertSort(int[] a, int n)

{

for (int i = 0; i < n; i++)

{

int t = a[i];

for (int j = i - 1; j >= 0; j--)

{

if (t < a[j])

{

a[j + 1] = a[j];

a[j] = t;

}

else

{

break;

}

k++;

}

k++;

}

}

 

/// <summary>

/// 希尔排序

/// 希尔排序是将数组按照一定增量分为几组,然后对每组进行直接插入排序

/// 增量按照(n/2,n/4,...)以此递减,最后以增量为1进行最后排序,

/// 而直接插入排序就是直接将增量设置为1的希尔排序

/// </summary>

/// <param name="a">数组</param>

/// <param name="n">数组长度</param>

public static void ShellInsertSort(int[] a, int n)

{

for (double d = Math.Floor((double)(n / 2)); d >= 1; d = Math.Floor(d / 2))

{

ShellSort(a, n, (int)d);

k++;

}

}

 

/// <summary>

/// 按照增量d进行快速插入排序

/// </summary>

/// <param name="a">数组</param>

/// <param name="n">数组长度</param>

/// <param name="d">增量</param>

public static void ShellSort(int[] a, int n, int d)

{

for (int m = 0; m < d; m++)

{

for (int i = d + m; i < n; i = i + d)

{

int t = a[i];

for (int j = i - d; j >= 0; j = j - d)

{

if (t < a[j])

{

a[j + d] = a[j];

a[j] = t;

}

k++;

}

k++;

}

k++;

}

}

}

}

 

交换排序(冒泡排序&快速排序)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Sort

{

public class ExchangeSort

{

public static int k = 0;

/// <summary>

/// 冒泡排序

/// </summary>

/// <param name="a">数组</param>

/// <param name="n">数组长度</param>

public static void BubbleSort(int[] a, int n)

{

for (int m = 1; m <= n; m++)

{

int j = 0;

for (int i = 0; i < n - m; i++)

{

if (a[i] > a[i + 1])

{

int t = a[i];

a[i] = a[i + 1];

a[i + 1] = t;

j++;

}

k++;//计数

}

k++;

if (j == 0)

break; //如果J等于0,则表示在这一趟排序中,没有进行数据交换,则说明已经排好序列

}

}

 

/// <summary>

/// 快速排序

/// </summary>

/// <param name="a">数组</param>

/// <param name="n">数组长度</param>

public static void QuickSort(int[] a, int n)

{

QSort(a, 0, n - 1);

}

 

/// <summary>

/// 递归进行快速排序

/// </summary>

/// <param name="a">数组</param>

/// <param name="low">最低索引号</param>

/// <param name="high">最高索引号</param>

public static void QSort(int[] a, int low, int high)

{

if (low < high)

{

int i = low;

int j = high;

int t = a[i];

while (i < j)

{

while (i < j && a[j] >= t)

{

j--;

k++; //计数

}

a[i] = a[j];

while (i < j && a[i] <= t)

{

i++;

k++;

}

a[j] = a[i];

k++;

}

if (i == j)

{

a[i] = t;

QSort(a, low, i - 1);

QSort(a, i + 1, high);

}

}

}

}

}

选择排序(简单选择排序&堆排序)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Sort

{

public class SelectionSort

{

public static int k = 0;

 

/// <summary>

/// 简单选择排序

/// </summary>

/// <param name="a">数组</param>

/// <param name="n">数组长度</param>

public static void SimpleSelectionSort(int[] a, int n)

{

for (int i = 0; i < n; i++)

{

int t = a[i];

for (int j = i + 1; j < n; j++)

{

if (a[j] < t)

{

a[i] = a[j];

a[j] = t;

t = a[i];

}

k++;

}

k++;

}

}

 

/// <summary>

/// 堆排序

/// 第一步:建立堆

/// 第二步:将建立好的堆的第一个数取出放到最后一位,然后对前面重新建立堆,然后再取出第一个最大数

/// </summary>

/// <param name="a"></param>

/// <param name="m"></param>

public static void HeapSort(int[] a, int n)

{

for (int j = n; j > 1; j--)

{

for (int i = j / 2 - 1; i >= 0; i--)

{

CreateHeap(a, i, j - 1);

}

int t = a[0];

a[0] = a[j - 1];

a[j - 1] = t;

k++;

}

}

 

/// <summary>

/// 建立堆(大顶堆)

/// </summary>

/// <param name="a">数组</param>

/// <param name="low">数组的起始结点索引</param>

/// <param name="high">数组的最大索引</param>

public static void CreateHeap(int[] a, int low, int high)

{

int i = low;

int j = 2 * i + 1;

int t = a[i];

while (j <= high)

{

k++;

if (j + 1 <= high && a[j] < a[j + 1])//左孩子小于右孩子

{

j++;//指向右孩子

}

if (a[i] < a[j])

{

a[i] = a[j];

a[j] = t;

i = j;//以交换后的孩子结点为根节点,继续进行创建堆

j = 2 * i + 1;

}

else

{

break;

}

}

}

}

}

归并排序

/// <summary>

/// 合并排序

/// </summary>

/// <param name="a">数组</param>

/// <param name="low">最低索引</param>

/// <param name="high">最高索引</param>

private static void MergeSort(int[] a,int low,int high)

{

if (low<high)

{

int mid = low+(high-low)/2;

MergeSort(a, low, mid);

MergeSort(a, mid + 1, high);

Merge(a, low,mid,high);

}

}

 

/// <summary>

/// 将数组合并

/// </summary>

/// <param name="a"></param>

/// <param name="low"></param>

/// <param name="mid"></param>

/// <param name="high"></param>

private static void Merge(int[] a,int low,int mid,int high)

{

int[] b = new int[high - low + 1];

int i = low, j = mid + 1, k = 0;

while (i <= mid && j <= high)

{

b[k++] = (a[i] < a[j]) ? a[i++] : a[j++];

}

while (i <= mid)

{

b[k++] = a[i++];

}

while (j <= high)

{

b[k++] = a[j++];

}

for (k = 0, i = low; i <= high; k++, i++)

{

a[i] = b[k];

}

}

 

主程序:

private static int k = 0;

static void Main(string[] args)

{

int[] a = new int[17] { 18, 15, 14,100, 13, 3, 4, 5, 10, 2, 7, 21, 9, 8, 23, 54, 30 };

Print(a);

//Console.WriteLine("直接插入排序:");

//InsertSort.DirectInsertSort(a, a.Length);

ShellSort(a, a.Length, 1);

//Print(a);

//Console.WriteLine("直接插入排序需要 {0} 次计算。", InsertSort.k);

 

//Console.WriteLine("希尔排序:");

//InsertSort.ShellInsertSort(a, a.Length);

//Print(a);

//Console.WriteLine("希尔排序需要 {0} 次计算。", InsertSort.k);

 

//Console.WriteLine("冒泡排序:");

//ExchangeSort.BubbleSort(a, a.Length);

//Print(a);

//Console.WriteLine("冒泡排序需要 {0} 次计算。", ExchangeSort.k);

 

//Console.WriteLine("快速排序:");

//ExchangeSort.QuickSort(a, a.Length);

//Print(a);

//Console.WriteLine("快速排序需要 {0} 次计算。", ExchangeSort.k);

 

//Console.WriteLine("简单选择排序:");

//SelectionSort.SimpleSelectionSort(a, a.Length);

//Print(a);

//Console.WriteLine("简单选择排序需要 {0} 次计算。", SelectionSort.k);

 

//Console.WriteLine("堆排序:");

//SelectionSort.HeapSort(a, a.Length);

//Print(a);

//Console.WriteLine("堆排序需要 {0} 次计算。", SelectionSort.k);

 

Console.WriteLine("合并排序:");

MergeSort(a, 0,a.Length-1);

Print(a);

Console.WriteLine("合并排序需要 {0} 次计算。", SelectionSort.k);

 

Console.ReadLine();

}

转载于:https://www.cnblogs.com/The-SEA/p/4647769.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值