C#实现所有经典排序算法
1、选择排序

class SelectionSorter
{
private int min;
public void Sort(int[] arr)
{
for (int i = ; i < arr.Length - ; ++i)
{
min = i;
for (int j = i + ; j < arr.Length; ++j)
{
if (arr[j] < arr[min])
min = j;
}
int t = arr[min];
arr[min] = arr[i];
arr[i] = t;
}
}
}

2、冒泡排序

class EbullitionSorter
{
public void Sort(int[] arr)
{
int i, j, temp;
bool done = false;
j = ;
while ((j < arr.Length) && (!done))//判断长度
{
done = true;
for (i = ; i < arr.Length - j; i++)
{
if (arr[i] > arr[i + ])
{
done = false;
temp = arr[i];
arr[i] = arr[i + ];//交换数据
arr[i + ] = temp;
}
}
j++;
}
}
}

3、快速排序

class QuickSorter
{
private void swap(ref int l, ref int r)
{
int temp;
temp = l;
l = r;
r = temp;
}
public void Sort(int[] list, int low, int high)
{
int pivot;//存储分支点
int l, r;
int mid;
if (high <= low)
return;
else if (high == low + )
{
if (list[low] > list[high])
swap(ref list[low], ref list[high]);
return;
}
mid = (low + high) >> ;
pivot = list[mid];
swap(ref list[low], ref list[mid]);
l = low + ;
r = high;
do
{
while (l <= r && list[l] < pivot)
l++;
while (list[r] >= pivot)
r--;
if (l < r)
swap(ref list[l], ref list[r]);
} while (l < r);
list[low] = list[r];
list[r] = pivot;
if (low + < r)
Sort(list, low, r - );
if (r + < high)
Sort(list, r + , high);
}
}

4、插入排序

public class InsertionSorter
{
public void Sort(int[] arr)
{
for (int i = ; i < arr.Length; i++)
{
int t = arr[i];
int j = i;
while ((j > ) && (arr[j - ] > t))
{
arr[j] = arr[j - ];//交换顺序
--j;
}
arr[j] = t;
}
}
}

5、希尔排序

public class ShellSorter
{
public void Sort(int[] arr)
{
int inc;
for (inc = ; inc <= arr.Length / ; inc = * inc + ) ;
for (; inc > ; inc /= )
{
for (int i = inc + ; i <= arr.Length; i += inc)
{
int t = arr[i - ];
int j = i;
while ((j > inc) && (arr[j - inc - ] > t))
{
arr[j - ] = arr[j - inc - ];//交换数据
j -= inc;
}
arr[j - ] = t;
}
}
}
}

6、归并排序

     /// <summary>
/// 归并排序之归:归并排序入口
/// </summary>
/// <param name="data">无序的数组</param>
/// <returns>有序数组</returns>
/// <author>Lihua(www.zivsoft.com)</author>
int[] Sort(int[] data)
{
//取数组中间下标
int middle = data.Length / ;
//初始化临时数组let,right,并定义result作为最终有序数组
int[] left = new int[middle], right = new int[middle], result = new int[data.Length];
if (data.Length % != )//若数组元素奇数个,重新初始化右临时数组
{
right = new int[middle + ];
}
if (data.Length <= )//只剩下1 or 0个元数,返回,不排序
{
return data;
}
int i = , j = ;
foreach (int x in data)//开始排序
{
if (i < middle)//填充左数组
{
left[i] = x;
i++;
}
else//填充右数组
{
right[j] = x;
j++;
}
}
left = Sort(left);//递归左数组
right = Sort(right);//递归右数组
result = Merge(left, right);//开始排序
//this.Write(result);//输出排序,测试用(lihua debug)
return result;
}
/// <summary>
/// 归并排序之并:排序在这一步
/// </summary>
/// <param name="a">左数组</param>
/// <param name="b">右数组</param>
/// <returns>合并左右数组排序后返回</returns>
int[] Merge(int[] a, int[] b)
{
//定义结果数组,用来存储最终结果
int[] result = new int[a.Length + b.Length];
int i = , j = , k = ;
while (i < a.Length && j < b.Length)
{
if (a[i] < b[j])//左数组中元素小于右数组中元素
{
result[k++] = a[i++];//将小的那个放到结果数组
}
else//左数组中元素大于右数组中元素
{
result[k++] = b[j++];//将小的那个放到结果数组
}
}
while (i < a.Length)//这里其实是还有左元素,但没有右元素
{
result[k++] = a[i++];
}
while (j < b.Length)//右右元素,无左元素
{
result[k++] = b[j++];
}
return result;//返回结果数组
}
注:此算法由周利华提供(http://www.cnblogs.com/architect/archive/2009/05/06/1450489.html

7、基数排序

  //基数排序
public int[] RadixSort(int[] ArrayToSort, int digit)
{
//low to high digit
for (int k = ; k <= digit; k++)
{
//temp array to store the sort result inside digit
int[] tmpArray = new int[ArrayToSort.Length];
//temp array for countingsort
int[] tmpCountingSortArray = new int[]{,,,,,,,,,};
//CountingSort
for (int i = ; i < ArrayToSort.Length; i++)
{
//split the specified digit from the element
int tmpSplitDigit = ArrayToSort[i]/(int)Math.Pow(,k-) - (ArrayToSort[i]/(int)Math.Pow(,k))*;
tmpCountingSortArray[tmpSplitDigit] += ;
}
for (int m = ; m < ; m++)
{
tmpCountingSortArray[m] += tmpCountingSortArray[m - ];
}
//output the value to result
for (int n = ArrayToSort.Length - ; n >= ; n--)
{
int tmpSplitDigit = ArrayToSort[n] / (int)Math.Pow(,k - ) - (ArrayToSort[n]/(int)Math.Pow(,k)) * ;
tmpArray[tmpCountingSortArray[tmpSplitDigit]-] = ArrayToSort[n];
tmpCountingSortArray[tmpSplitDigit] -= ;
}
//copy the digit-inside sort result to source array
for (int p = ; p < ArrayToSort.Length; p++)
{
ArrayToSort[p] = tmpArray[p];
}
}
return ArrayToSort;
}

8、计数排序

//计数排序
/// <summary>
/// counting sort
/// </summary>
/// <param name="arrayA">input array</param>
/// <param name="arrange">the value arrange in input array</param>
/// <returns></returns>
public int[] CountingSort(int[] arrayA, int arrange)
{
//array to store the sorted result,
//size is the same with input array.
int[] arrayResult = new int[arrayA.Length];
//array to store the direct value in sorting process
//include index 0;
//size is arrange+1;
int[] arrayTemp = new int[arrange+];
//clear up the temp array
for(int i = ; i <= arrange; i++)
{
arrayTemp[i] = ;
}
//now temp array stores the count of value equal
for(int j = ; j < arrayA.Length; j++)
{
arrayTemp[arrayA[j]] += ;
}
//now temp array stores the count of value lower and equal
for(int k = ; k <= arrange; k++)
{
arrayTemp[k] += arrayTemp[k - ];
}
//output the value to result
for (int m = arrayA.Length-; m >= ; m--)
{
arrayResult[arrayTemp[arrayA[m]] - ] = arrayA[m];
arrayTemp[arrayA[m]] -= ;
}
return arrayResult;
}

9、小根堆排序

/// <summary>
/// 小根堆排序
/// </summary>
/// <param name="dblArray"></param>
/// <param name="StartIndex"></param>
/// <returns></returns> private void HeapSort(ref double[] dblArray)
{
for (int i = dblArray.Length - ; i >= ; i--)
{
if ( * i + < dblArray.Length)
{
int MinChildrenIndex = * i + ;
//比较左子树和右子树,记录最小值的Index
if ( * i + < dblArray.Length)
{
if (dblArray[ * i + ] > dblArray[ * i + ])
MinChildrenIndex = * i + ;
}
if (dblArray[i] > dblArray[MinChildrenIndex])
{ ExchageValue(ref dblArray[i], ref dblArray[MinChildrenIndex]);
NodeSort(ref dblArray, MinChildrenIndex);
}
}
}
} /// <summary>
/// 节点排序
/// </summary>
/// <param name="dblArray"></param>
/// <param name="StartIndex"></param> private void NodeSort(ref double[] dblArray, int StartIndex)
{
while ( * StartIndex + < dblArray.Length)
{
int MinChildrenIndex = * StartIndex + ;
if ( * StartIndex + < dblArray.Length)
{
if (dblArray[ * StartIndex + ] > dblArray[ * StartIndex + ])
{
MinChildrenIndex = * StartIndex + ;
}
}
if (dblArray[StartIndex] > dblArray[MinChildrenIndex])
{
ExchageValue(ref dblArray[StartIndex], ref dblArray[MinChildrenIndex]);
StartIndex = MinChildrenIndex;
}
}
} /// <summary>
/// 交换值
/// </summary>
/// <param name="A"></param>
/// <param name="B"></param>
private void ExchageValue(ref double A, ref double B)
{
double Temp = A;
A = B;
B = Temp;
}

C#实现所有经典排序算法汇总的更多相关文章

  1. JavaScript 数据结构与算法之美 - 十大经典排序算法汇总(图文并茂)

    1. 前言 算法为王. 想学好前端,先练好内功,内功不行,就算招式练的再花哨,终究成不了高手:只有内功深厚者,前端之路才会走得更远. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 ...

  2. C#所有经典排序算法汇总

    1.选择排序 选择排序 class SelectionSorter     {         private int min;         public void Sort(int[] arr) ...

  3. 排序算法汇总(C/C++实现)

    前言:     本人自接触算法近2年以来,在不断学习中越多地发觉各种算法中的美妙.之所以在这方面过多的投入,主要还是基于自身对高级程序设计的热爱,对数学的沉迷.回想一下,先后也曾参加过ACM大大小小的 ...

  4. 经典排序算法 – 插入排序Insertion sort

    经典排序算法 – 插入排序Insertion sort  插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕. 插入排序方法分直接插入排序和折半插入排序两种, ...

  5. 经典排序算法总结与实现 ---python

    原文:http://wuchong.me/blog/2014/02/09/algorithm-sort-summary/ 经典排序算法在面试中占有很大的比重,也是基础,为了未雨绸缪,在寒假里整理并用P ...

  6. 经典排序算法及python实现

    今天我们来谈谈几种经典排序算法,然后用python来实现,最后通过数据来比较几个算法时间 选择排序 选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理是每一次从待排序的数据 ...

  7. 经典排序算法 - 基数排序Radix sort

    经典排序算法 - 基数排序Radix sort 原理类似桶排序,这里总是须要10个桶,多次使用 首先以个位数的值进行装桶,即个位数为1则放入1号桶,为9则放入9号桶,临时忽视十位数 比如 待排序数组[ ...

  8. 经典排序算法 - 高速排序Quick sort

    经典排序算法 - 高速排序Quick sort 原理,通过一趟扫描将要排序的数据切割成独立的两部分,当中一部分的全部数据都比另外一部分的全部数据都要小,然后再按此方法对这两部分数据分别进行高速排序,整 ...

  9. 经典排序算法 - 归并排序Merge sort

    经典排序算法 - 归并排序Merge sort 原理,把原始数组分成若干子数组,对每个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到所有合并完,形成有序的数组 举例 无序数组[6 2 ...

随机推荐

  1. 【WIP】Bootstrap 基础

    创建: 2017/09/28   更新: 2017/10/14 标题加上[WIP]

  2. 洛谷P1726 上白泽慧音(Tarjan强连通分量)

    P1726 上白泽慧音 题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村 ...

  3. Treap(模板)

    人生第一次平衡树,Treap板子 #include<iostream> #include<cstdio> #include<cstring> #include< ...

  4. [App Store Connect帮助]三、管理 App 和版本(6.2)转让 App:App 转让条件

    App 必须满足以下条件才可转让: 转让者和接收者的帐户均不处于待处理或变更状态,且双方均已接受“协议.税务和银行业务”部分中最新版本的转让协议 该 App 的任何版本都不能使用 iCloud 权限 ...

  5. [Apple开发者帐户帮助]九、参考(4)支持的功能(macOS)

    macOS应用程序可用的功能取决于您的程序成员身份和签名证书. 能力 ADP 开发者ID Apple开发者 应用程序组 App沙盒 游戏中心     硬化运行时 iCloud:CloudKit   i ...

  6. [Swift通天遁地]六、智能布局-(1)给视图添加尺寸和中心点的约束

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. android GPS 定位,取位置信息

    现在很多app ,需要取位置信息,所以我也做了一个模块用来取位置信息:   加入位置服务所需的权限: <uses-permission android:name="android.pe ...

  8. MVC系列学习(十一)-客户端的验证

    1.通过一个实例,来了解MVC中强大的验证功能 1.1新建一个 [基本] 的mvc项目,因为要用到验证的js,然后在一个视图中写上一下代码,以及Model中的代码如下 [注]在调用html.EditF ...

  9. jQuery——类的添加与删除

    添加类:addClass 删除类:removeClass 判断类是否存在:hasClass <!DOCTYPE html> <html lang="en"> ...

  10. SQL基本操作——日期函数

    SQL日期:当我们处理日期时,最难的任务恐怕是确保所插入的日期的格式,与数据库中日期列的格式相匹配.只要数据包含的只是日期部分,运行查询就不会出问题.但是,如果涉及时间,情况就有点复杂了.在讨论日期查 ...