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;
        }

  

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

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

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

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

    C#实现所有经典排序算法1.选择排序 class SelectionSorter { private int min; public void Sort(int[] arr) { ; i < a ...

  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. 通过存储过程(SP)实现SQL Server链接服务器(LinkServer)的添加

    1. 背景 当系统的微服务化做的不是很高的时候,部分功能要通过DB LinkServer 来实现跨 Server 查询,当然,有时候BI抽数据.DBA数据库维护可能也会创建LinkServer. 特别 ...

  2. MATLAB最大均值差异(Maximum Mean Discrepancy)

    MATLAB最大均值差异(Maximum Mean Discrepancy) 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 更多内容,请看标签:MAT ...

  3. juc-2-原子变量与CAS算法

      i++的原子性问题 例子   int i=10;           i++; 结果   i=10   分析过程                    在计算机 底层 会有生成一个临时变量 tem ...

  4. 【bzoj4945】[Noi2017]游戏(搜索+2-sat)

    bzoj 洛谷 题意: 现在有\(a,b,c\)三种车,每个赛道可能会存在限制:\(a\)表示不能选择\(a\)类型的赛车,\(b,c\)同理:\(x\)表示该赛道不受限制,但\(x\)类型的个数$\ ...

  5. QT向界面中嵌套新的界面

    简单说下 想要实现的内容 我们有一个主窗口mainwindow,需要向其中放入新的界面,你可以自己定义里面内容. 大致的效果图如下 实现起来就是利用QT的layout布局 使用水平布局QHboxlay ...

  6. Paper | PyTorch: An Imperative Style, High-Performance Deep Learning Library

    目录 0. 摘要 1. 简介 2. 背景 3. 设计原则 4. 针对易用性的核心设计 4.1 让深度学习模块不过是Python程序 4.2 互用性和可拓展性 4.3 自动差分 5. 针对高性能的PyT ...

  7. 如何创建Azure Face API和计算机视觉Computer Vision API

    在人工智能技术飞速发展的当前,利用技术手段实现人脸识别.图片识别已经不是什么难事.目前,百度.微软等云计算厂商均推出了人脸识别和计算机视觉的API,其优势在于不需要搭建本地环境,只需要通过网络交互,就 ...

  8. ReactNative: 使用View组件创建九宫格

    一.简言 初学RN,一切皆新.View组件跟我们iOS中UIView类似,作为一个容器视图使用,它主要负责承载其他的子组件.View组件采用的是FlexBox伸缩盒子布局,通过对它的布局可以影响子组件 ...

  9. React: 研究React的组件化

    一.简介大概 在以往的Web开发中,会把web页面所有的复杂控件作为一个单一的整体进行开发,由于控件之间需要进行通信,因此不同的组件之间的耦合度会很多,由于开发一个控件的时候要考虑到控件与控件之间的联 ...

  10. 03-模板(过滤器,代码复用,表单,CSRF)

    模块代码复用 在模板中,可能会遇到以下情况: 多个模板具有完全相同的顶部和底部内容 多个模板中具有相同的模板代码内容,但是内容中部分值不一样 多个模板中具有完全相同的 html 代码块内容 像遇到这种 ...