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

选择排序
class SelectionSorter   
{   
    private int min;   
    public void Sort(int[] arr)   
    {   
        for (int i = 0; i < arr.Length - 1; ++i)   
        {   
            min = i;   
            for (int j = i + 1; 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 = 1;   
        while ((j < arr.Length) && (!done))//判断长度   
        {   
            done = true;   
            for (i = 0; i < arr.Length - j; i++)   
            {   
                if (arr[i] > arr[i + 1])   
                {   
                    done = false;   
                    temp = arr[i];   
                    arr[i] = arr[i + 1];//交换数据   
                    arr[i + 1] = 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 + 1)   
        {   
            if (list[low] > list[high])   
                swap(ref list[low], ref list[high]);   
            return;   
        }   
        mid = (low + high) >> 1;   
        pivot = list[mid];   
        swap(ref list[low], ref list[mid]);   
        l = low + 1;   
        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 + 1 < r)   
            Sort(list, low, r - 1);   
        if (r + 1 < high)   
            Sort(list, r + 1, high);   
    }     
}

4、插入排序

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

5、希尔排序

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

6、归并排序

归并排序
        /// <summary>
        /// 归并排序之归:归并排序入口
        /// </summary>
        /// <param name="data">无序的数组</param>
        /// <returns>有序数组</returns>
        /// <author>Lihua(www.zivsoft.com)</author>
        int[] Sort(int[] data)
        {
            //取数组中间下标
            int middle = data.Length / 2;
            //初始化临时数组let,right,并定义result作为最终有序数组
            int[] left = new int[middle], right = new int[middle], result = new int[data.Length];
            if (data.Length % 2 != 0)//若数组元素奇数个,重新初始化右临时数组
            {
                right = new int[middle + 1];
            }
            if (data.Length <= 1)//只剩下1 or 0个元数,返回,不排序
            {
                return data;
            }
            int i = 0, j = 0;
            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 = 0, j = 0, k = 0;
            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 = 1; 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[10]{0,0,0,0,0,0,0,0,0,0};       
                //CountingSort       
                for (int i = 0; i < ArrayToSort.Length; i++)       
                {          
                    //split the specified digit from the element
                    int tmpSplitDigit = ArrayToSort[i]/(int)Math.Pow(10,k-1) - (ArrayToSort[i]/(int)Math.Pow(10,k))*10;
                    tmpCountingSortArray[tmpSplitDigit] += 1;
                }        
                for (int m = 1; m < 10; m++)     
                {           
                    tmpCountingSortArray[m] += tmpCountingSortArray[m - 1];       
                }       
                //output the value to result     
                for (int n = ArrayToSort.Length - 1; n >= 0; n--)      
                {          
                    int tmpSplitDigit = ArrayToSort[n] / (int)Math.Pow(10,k - 1) - (ArrayToSort[n]/(int)Math.Pow(10,k)) * 10;          
                    tmpArray[tmpCountingSortArray[tmpSplitDigit]-1] = ArrayToSort[n];           
                    tmpCountingSortArray[tmpSplitDigit] -= 1;      
                }       
                //copy the digit-inside sort result to source array      
                for (int p = 0; 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+1];   
            //clear up the temp array   
            for(int i = 0; i <= arrange; i++)   
            {       
                arrayTemp[i] = 0;
            }   
            //now temp array stores the count of value equal
            for(int j = 0; j < arrayA.Length; j++)  
            {      
                arrayTemp[arrayA[j]] += 1;  
            }   
            //now temp array stores the count of value lower and equal
            for(int k = 1; k <= arrange; k++)  
            {      
                arrayTemp[k] += arrayTemp[k - 1];
            }    
            //output the value to result   
            for (int m = arrayA.Length-1; m >= 0; m--)  
            {       
                arrayResult[arrayTemp[arrayA[m]] - 1] = arrayA[m];   
                arrayTemp[arrayA[m]] -= 1;
            }   
            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 - 1; i >= 0; i--)
            {
                if (2 * i + 1 < dblArray.Length)
                {
                    int MinChildrenIndex = 2 * i + 1;
                    //比较左子树和右子树,记录最小值的Index
                    if (2 * i + 2 < dblArray.Length)
                    {
                        if (dblArray[2 * i + 1] > dblArray[2 * i + 2])
                            MinChildrenIndex = 2 * i + 2;
                    }
                    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 (2 * StartIndex + 1 < dblArray.Length)
            {
                int MinChildrenIndex = 2 * StartIndex + 1;
                if (2 * StartIndex + 2 < dblArray.Length)
                {
                    if (dblArray[2 * StartIndex + 1] > dblArray[2 * StartIndex + 2])
                    {
                        MinChildrenIndex = 2 * StartIndex + 2;
                    }
                }
                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;
        }

转载于:https://www.cnblogs.com/nwxfy/archive/2012/03/08/sorting.html

C#中排序的多种实现方式相关推荐

  1. Python去除字符串中某个字符多种实现方式对比

    1.如何去掉字符串中不需要的字符? 实际案例: (1)过滤掉用户输入前后多余的空白字符:' nick2008@gmail.com ' (2)过滤某windows下编辑文本中的'\r':'hello w ...

  2. Android开发中怎样调用系统Email发送邮件(多种调用方式)

    在Android中调用其他程序进行相关处理,几乎都是使用的Intent,所以,Email也不例外,所谓的调用Email,只是说Email可以接收Intent并做这些事情 我们都知道,在Android中 ...

  3. c语言实验题——字符串排序,C语言中实现“三个数由小到大排序”的多种方法浅析...

    本文通过一个简单示例"三个数由小到大排序",将C语言中许多知识点融会贯通起来,这多种方法的实现可以将函数.宏.指针之间的区别和本质清晰的展示给读者,使本来很复杂难以理解的概念变得通 ...

  4. 云呐|固定资产盘点中,支持多种盘点方式(资产清查盘点)

    高效盘点是条形码扫描和RFID技术固定资产管理系统的主要特点,但结合用户咨询,云呐发现许多人不知道如何根据条形码或RFID标签实现固定资产管理系统的资产盘点. 处理固定资产管理系统的流程可以协助不了解 ...

  5. 一篇文章带你看懂以及实现加解密技术中的信息防篡改、一码一检、过期失效、多种实现方式

    一篇文章带你看懂以及实现加解密技术中的信息防篡改.一码一检.过期失效.多实现方式 导语 一.简介 二.代码功能介绍以及源码 2.1.AbstractRsa 类 2.2 RsaUtils 类 2.3 R ...

  6. MATLAB遗传算法GA求解TSP旅行商问题,可选PMX交叉、OX交叉及其它多种交叉方式,在算法中引入2-opt变异算子

    MATLAB遗传算法GA求解TSP旅行商问题,可选PMX交叉.OX交叉及其它多种交叉方式,在算法中引入2-opt变异算子.进化逆转算子提高算法局部搜索能力,利用国际通用的TSPLIB数据集中的eil5 ...

  7. 07实战之电商网站商品管理:多种搜索方式

    作为准备工作,又重新恢复了之前的3个商品文档 本节主要演示多种搜索方式,关于各种搜索语法后续会详细讲解. 1.query string search 之前也用过这个命令 , 搜索全部商品: GET / ...

  8. SQL Server 与 MySQL 中排序规则与字符集相关知识的一点总结

    字符集&&排序规则 字符集是针对不同语言的字符编码的集合,比如UTF-8字符集,GBK字符集,GB2312字符集等等,不同的字符集使用不同的规则给字符进行编码.排序规则则是在特定字符集 ...

  9. powerbi输入数据_Power BI 的多种共享方式

    学而时习之 学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎? -- 孔子<论语> 据数据统计,通过温习可掌握80%的所学知识点,为此我们将已推出的课程整理为教程文章 ...

最新文章

  1. dynamic.rnn()sequence_len理解
  2. 法国呼叫服务公司Aircall获得800万美元融资
  3. html css文本框按钮,css样式之区分input是按钮还是文本框的方法
  4. tf.train.examle函数
  5. 用python倒序输出一个字符串_Python字符串逆序输出的实例讲解
  6. (读书笔记).NET大局观-.NET框架类库概观
  7. Altium Desiger18 打印 丝印简单的方法
  8. mysql如果数据不存在,则插入新数据,否则更新
  9. [ASP.NET Core 3框架揭秘] 跨平台开发体验: Windows [上篇]
  10. sql相同顺序法和一次封锁法_不到75行代码,导出最高法指导案例到excel(一)...
  11. 在TOMCAT中使用JNDI连接数据源
  12. NAACL2021论文:UniDrop:一种简单而有效的Transformer提升技术
  13. [笔记]远传中继的实现
  14. QT的自动滚动区QScrollArea的用法,图文详解
  15. 探索 ES8 Object.entries()
  16. 玩客云刷上Armbian的体验
  17. Unity3D游戏开发入门学习笔记
  18. 全网最简单的百度网盘提速方法!!!!
  19. Unity加载优化-将基于LZMA的ab压缩方案修改为LZ4压缩的过程
  20. 推荐4款非常实用的电脑软件

热门文章

  1. Java多线程2:Thread中的实例方法
  2. java 面试题汇总
  3. SQL Server 中数据查询注意事项
  4. Python 项目实践二(下载数据)第四篇
  5. 【启发式搜索】[ZOJ1217]Eight
  6. PHP 在 Nginx 下主动断开连接 Connection Close 与 ignore_user_abort 后台运行
  7. Android中的“再按一次返回键退出程序”实现
  8. 用STL给C++充电:第一部分
  9. Activity栈管理(一):Activity任务栈模型
  10. 算法----有效的括号