算法学习-常用排序

他家都说:“程序员到最后比的是算法”,我对算法也算情有独钟,最近这些天,没事的时候就看看算法相关的书,感觉还是算法能体现一个程序员的能力,今天学习了常用的排序算法,不好意思还是C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SuanFa
{
    public class SortMethod
    {
        //插入排序
        //把数B与其前面的数比较,如果B比其前面的数C小,
        //那么C到B之间的数都向后移一位,C所在的位置由B代替
        public bool InsertSort(int[] A, int n)
        {
            int sortData;
            for (int i = 1; i < n; i++)
            {
                sortData = A[i];
                for (int j = 0; j < i; j++)//循环一次只为插进一个数,循环n-1次即可
                {
                    if (A[j] > sortData)
                    {
                        for (int k = i; k > j; k--)//j-i之间的数向后移一位
                        {
                            A[k] = A[k - 1];
                        }
                        A[j] = sortData;
                        break;//break在这里很重要,如果没有break,你看看A[j]=sortData;就执行了i-k次,
                        // 排序后就会出现同一个数出现多次,当然有的数就去掉了。
                    }
                }
            }

            return true;
        }

        //冒泡排序
        public bool BubbleSort(int[] A, int n)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    if (A[j] > A[i])
                    {
                        int tmp = A[j];
                        A[j] = A[i];
                        A[i] = tmp;
                    }
                }
            }

            return true;
        }

        //选择排序
        //把当前要排序的那个数与它后面的数进行比较,如果哪个数更小,就让这个更小的输入与后面的数进行比较,
        //一直比较下去,将当前要排序的数与更小的那个数交换
        public bool SelectSort(int[] A, int n)
        {
            for (int i = 0; i < n - 1; i++)
            {
                int k = i;
                for (int j = i + 1; j < n; j++)
                {
                    if (A[j] < A[k])
                    {
                        k = j;
                    }
                }

                if (k != i)
                {
                    int tmp = A[k];
                    A[k] = A[i];
                    A[i] = tmp;
                }
            }
            return true;
        }


        #region 合并排序
        /// <param name="A">被排序的数组</param>
        /// <param name="l">开始排序的下标</param>
        /// <param name="r">结束排序的下标</param>
        /// <param name="n">数组最后一个元素的下标</param>
        private void Merge(int[] A, int e, int r, int n)
        {
            int n1 = r - e ;
            int n2 = n - r+1;
            int[] e1 = new int[n1];
            int[] r1 = new int[n2];
            for (int t = 0; t < n1; t++)
            {
                e1[t] = A[e + t];
            }
            for (int t = 0; t < n2; t++)
            {
                r1[t] = A[r + t];
            }
            int i = 0, j = 0, k = e;
            while (i < n1 && j < n2)
            {
                if (e1[i] <= r1[j])
                {
                    A[k++] = e1[i++];
                }
                else
                {
                    A[k++] = r1[j++];
                }
            }
            while (i < n1)
            {
                A[k++] = e1[i++];
            }
            while (j < n2)
            {
                A[k++] = r1[j++];
            }
        }

        public void MergeSort(int[] A, int r, int n)
        {
            if (r < n)
            {
               int p=(n+r)/2;
               MergeSort(A,r,p);
               MergeSort(A,p+1,n);
               Merge(A,r,p,n);
            }
        }
        #endregion

        //合并两个数组,合并排序的思想就是合并两个数组
        public int[] MergeTwo(int[] A, int[] B)
        {
            int n1 = A.Length;
            int n2 = B.Length;
            int[] C = new int[n1 + n2];
            int i=0,j =0,k=0;
            while (i < n1 && j < n2)
            {
                if (A[i] > B[j])
                {
                    C[k++] = B[j++];
                }
                else {
                    C[k++] = A[i++];
                }
            }
            while (i < n1)
            {
                C[k++] = A[i++];
            }

            while (j < n2)
            {
                C[k++] = B[j++];
            }
            return C;
        }

 

       //哈希算法

       //老实说这段代码不是我写的,哈希算法的确有点难懂,但我还是会他搞定
        public void HillSort(int[] list)
        {
            int inc;
            for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
            
            for (; inc > 0; inc /= 3)
            {
                for (int i = inc + 1; i <= list.Length; i += inc)
                {
                    int t = list[i - 1];
                    int j = i;
                    while ((j > inc) && (list[j - inc - 1] > t))
                    {
                        list[j - 1] = list[j - inc - 1];
                        j -= inc;
                    }
                    list[j - 1] = t;
                }
            }
        }  
 
    }
}

你可能感兴趣的:(c,算法,list,Class,merge,n2)