排序演算法——C#語言

來源:互聯網
上載者:User

標籤:

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

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

}

排序演算法——C#語言

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.