C#实现所有经典排序算法



1、选择排序
选择排序
classSelectionSorter    
{    
    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、冒泡排序
所谓冒泡排序法,就是对一组数字进行从大到小或者从小到大排序的一种算法。具体方法是,相邻数值两两交换。从第一个数值开始,如果相邻两个数的排列顺序与我们的期望不同,则将两个数的位置进行交换(对调);如果其与我们的期望一致,则不用交换。重复这样的过程,一直到最后没有数值需要交换,则排序完成。一般地,如果有N个数需要排序,则需要进行(N-1)趟起泡,我们以从小到大排序为例来看一下
冒泡排序
classEbullitionSorter    
{    
    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、快速排序
快速排序
classQuickSorter    
{    
    private void swap(ref int l, ref intr)
{
    int temp;
    temp = l;
    l = r;
    r = temp;
}
public void Sort(int[] list, int low, inthigh)
{
    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], reflist[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], reflist[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 classInsertionSorter    
{    
    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、希尔排序
希尔排序基本思想:先取一个小于 n 的整数d1 作为第一个增量,把文件的全部记录分成 d1 个组。所有距离为d1的倍数的记录放在同一个组中。先在各组内进行直接插入排序;然后,取第二个增量 d2<d1重复上述的分组和排序,直至所取的增量 dt=1(dt<dt-l<…<d2<d1),即所有记录放在同一组中进行直接插入排序为止。
举例说明:
对于这样一个无序的数组 5 9 3 2 6 11 81 7 4 10 ,想把它变成顺序递增的数组
1 2 3 4 5 6 7 8 9 10 11。先隔3 个元素取一次:把 5 2 8 4 取了出来,往后搓一位,把9 6 1 10
取出来,再往后搓一位,又把 3 11 7 取出来。分别对这三个小组排序成为递增的序列,再插回去,如图:
于是得到了第一趟排序的结果:2 1 3 4 6 7 5 911 8 10.现在再以 2 为间隔重复以上步骤(这次得到的是两个小组)得到了2 1 3 4 5 7 6 8 11 9 10。最后再以 1 为间隔再搞一次(实际上这一步就是从左到右两两比较,调整位置),就得到了想要的结果。这就是希尔排序,其要义就是先进行宏观调整,再进行微观调整。
 
希尔排序
public classShellSorter    
{    
    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);//递归右数组             r
    esult = 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 sortresult inside digit
        int[] tmpArray = newint[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 digitfrom 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 toresult      
        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 sortresult to source array       
        for (int p = 0; p < ArrayToSort.Length; p++)
        {
            ArrayToSort[p] = tmpArray[p];
        }
    }
    return ArrayToSort;





8、计数排序
计数排序
//计数排序
/// <summary>
/// counting sort
/// </summary>
/// <paramname="arrayA">input array</param>         
/// <paramname="arrange">the value arrange in input array</param>
/// <returns></returns>
public int[] CountingSort(int[] arrayA, int arrange)
{
    //array to store the sortedresult,              
    //size is the samewith input array. 
    int[] arrayResult = newint[arrayA.Length];
    //array to store the direct value in sorting process  
    //include index 0;                 
    //size is arrange+1;    
    int[] arrayTemp = newint[arrange + 1];                 //clearup the temp array    
    for (int i = 0; i <= arrange; i++)
    {
        arrayTemp[i] = 0;
    }
    //now temp array stores the countof value equal 
    for (int j = 0; j < arrayA.Length; j++)
    {
        arrayTemp[arrayA[j]] += 1;
    }
    //now temp array stores the count of valuelower 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>
/// <paramname="dblArray"></param>
/// <paramname="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(refdblArray[i], ref dblArray[MinChildrenIndex]);
                NodeSort(ref dblArray, MinChildrenIndex);
            }
        }
    }
}
/// <summary>
/// 节点排序
/// </summary>
/// <paramname="dblArray"></param>         
/// <paramname="StartIndex"></param>
private void NodeSort(ref double[] dblArray, intStartIndex)
{
    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(refdblArray[StartIndex], ref dblArray[MinChildrenIndex]);
            StartIndex = MinChildrenIndex;
        }
    }
}
/// <summary>
/// 交换值
/// </summary>
/// <paramname="A"></param>        /// <param name="B"></param>
private void ExchageValue(ref double A, ref double B)
{
    double Temp = A;
    A = B;
    B = Temp;
}




                
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值