码迷,mamicode.com
首页 > 编程语言 > 详细

排序算法——C#语言版

时间:2015-07-14 13:02:19      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

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

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;

}

}

}

}

}

归并排序

 

主程序:

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.ReadLine();

}

排序算法——C#语言版

标签:

原文地址:http://www.cnblogs.com/The-SEA/p/4645069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!