(1)插入排序
using System;

namespace DataModle_InsertSort
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[]{ 1,4,5,3,12,89,56,12,54,47,32};
            Console.WriteLine("初始順序:");
            printData(arr);
            Console.WriteLine("排序結果(升序):");
            InsertSortAsc(arr);
            printData(arr);
            Console.WriteLine("排序結果(降序):");
            InsertSortDesc(arr);
            printData(arr);
            Console.Read();
        }

        /// <summary>
        /// 以升序排列
        /// </summary>
        /// <param name="arr"></param>
        public static void InsertSortAsc(int[] arr)
        {
            for (int i = 1; i < arr.Length; i++)
            {
               
                int temp = arr[i];
                int j=i-1;
                while (arr[j] >temp)
                {
                    arr[j + 1] = arr[j];
                    j--;
                    if (j == -1)
                    {
                        break;
                    }
                }
                arr[j + 1] = temp;
            }
        }

        /// <summary>
        /// 以降序排序
        /// </summary>
        /// <param name="arr"></param>
        public static void InsertSortDesc(int[] arr)
        {
            for (int i = 1; i < arr.Length; i++)
            {
                int temp = arr[i];
                int j = i - 1;
                while (arr[j] < temp)
                {
                    arr[j + 1] = arr[j];
                    j--;
                    if (j == -1)
                    {
                        break;
                    }
                }
                arr[j + 1] = temp;
            }

        }

        /// <summary>
        /// 輸出數據
        /// </summary>
        /// <param name="arr"></param>
        public static void printData(int[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i]+",");
            }
            Console.WriteLine();
        }
    }
}

(2)直接插入排序
using System;
using System.Collections.Generic;
using System.Text;

namespace DataSort
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] iArrary = new int[] { 5, 1, 13, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
            selectSort(iArrary);
            for (int m = 0; m < iArrary.Length; m++)
                Console.Write("{0} ", iArrary[m]);
            Console.WriteLine();
            MaxselectSort(iArrary);
            for (int m = 0; m < iArrary.Length; m++)
                Console.Write("{0} ", iArrary[m]);
            Console.ReadLine();

        }

       /// <summary>
        /// 从小到大
        /// </summary>
        /// <param name="List"></param>
        public static void selectSort(int[] List)
        {
            int i, j, small, temp;
            int n = List.Length;
            for (i = 0; i < n - 1; i++)
            {
                small = i;
                for (j = i + 1; j < n; j++)    //找出最小数的索引
                {
                    if (List[j] < List[small])
                        small = j;
                }
                if (small != i)
                {
                    temp = List[i];
                    List[i] = List[small];
                    List[small] = temp;
                }
            }
        }

        /// <summary>
        /// 从大到小
        /// </summary>
        /// <param name="List"></param>
        public static void MaxselectSort(int[] List)
        {
            int i, j, max, temp;
            int n = List.Length;
            for (i = 0; i < n - 1; i++)
            {
                max = i;
                for (j = i + 1; j < n; j++)    //找出最大数的索引
                {
                    if (List[j] > List[max])    //这里是找出比 List[i]还大的排前面
                        max = j;
                }
                if (max != i)
                {
                    temp = List[i];
                    List[i] = List[max];
                    List[max] = temp;
                }
            }
        }
    }
}

(3)希尔排序
using System;
using System.Collections.Generic;
using System.Text;

namespace ShellSort
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
            ShellSort(iArrary);
            for (int m = 0; m < iArrary.Length; m++)
                Console.Write("{0} ", iArrary[m]);
             Console.ReadLine();

        }

        public static void ShellSort(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;
                }
            }
        }
    }
}
(4)冒泡排序
using System;
using System.Collections.Generic;
using System.Text;

namespace MergeSort
{
      class Program
      {
          static void Main(string[] args)
         {
           

             int[] a = new int[] { 4, 2, 1, 6, 3, 6, 0, -5, 13, 10 };
             Console.Write("排序前:");
             for (int i = 0; i < a.Length; i++)
             {
                 Console.Write(a[i] + ",");
             }
             Sort(a);
             Console.WriteLine();
             Console.Write("排序后:");
            for (int i = 0; i < a.Length; i++)
             {
                Console.Write(a[i]+",");
             }
            Console.ReadLine();
         }
         /// <summary>
          /// 冒泡排序(1)
          /// </summary>
          /// <param name="num"></param>
          private static void Sort(int[] num)
          {
              for (int i = 0; i < num.Length; i++)
              {
                  for (int j = i + 1; j < num.Length ; j++)
                  {
                      if (num[i] > num[j])
                      {
                          int temp = num[i];
                          num[i] = num[j];
                          num[j] = temp;
                      }
                  }
              }
          }

          /// <summary>
          /// 冒泡排序(2)
          /// </summary>
          /// <param name="num"></param>
          private static void Sort2(int[] num)
          {
              for (int i = 0; i < num.Length - 1; i++)
              {
                  for (int j = num.Length - 1; j >= 1; j--)
                  {
                      if (num[j] < num[j - 1])
                      {
                          int temp = num[j];
                          num[j] = num[j - 1];
                          num[j - 1] = temp;
                      }

                  }
              }

          }
   }
}

(5)归并排序
using System;
using System.Collections.Generic;
using System.Text;

namespace MergeSort
{
      class Program
      {
          static void Main(string[] args)
         {
             Program p = new Program();

             int[] a = new int[] { 4, 2, 1, 6, 3, 6, 0, -5, 1, 10 };
             Console.Write("排序前:");
             for (int i = 0; i < a.Length; i++)
             {
                 Console.Write(a[i] + ",");
             }

             p.Sort(a);
             Console.WriteLine();
             Console.Write("排序后:");
            for (int i = 0; i < a.Length; i++)
             {
                Console.Write(a[i]+",");
             }
            Console.ReadLine();
         }

         /// <summary>
         /// 利用归并排序按由下到大的顺序排序
         /// </summary>
        /// <param name="toBeSort"></param>
         /// <returns></returns>
         public int[] Sort(int[] toBeSort)
         {
             if (toBeSort.Length == 0)
            {
                 throw new Exception("Sort array Error!");
             }

            if (toBeSort.Length > 1)
            {
                 int[] part1 = Sort(get1Part(toBeSort));
                 int[] part2 = Sort(get2Part(toBeSort));

                 merger(part1, part2, toBeSort);
             }

             return toBeSort;
         }

        /// <summary>
         /// 将part1和part2按照由小到大的顺序归并到数组toBeSort中
         /// </summary>
         /// <param name="part1"></param>
         /// <param name="part2"></param>
         /// <param name="toBeSort"></param>
        private void merger(int[] part1, int[] part2, int[] toBeSort)
         {
             int index = 0;
             int i1 = 0;
             int i2 = 0;

             while (i1 < part1.Length && i2 < part2.Length)
             {
                 if (part1[i1] < part2[i2])
                 {
                     toBeSort[index] = part1[i1];
                     i1++;
                 }
                 else
                 {
                     toBeSort[index] = part2[i2];
                     i2++;
                 }

                 index++;
             }

             while (i1 < part1.Length)
             {
                 toBeSort[index] = part1[i1];
                 index++;
                 i1++;
             }

             while (i2 < part2.Length)
            {
                 toBeSort[index] = part2[i2];
                 index++;
                 i2++;
             }
         }

         /// <summary>
         /// Get the second part of the array.
        /// </summary>
        /// <param name="toBeSort">The array to be sort.</param>
        /// <returns>The second part of the array.</returns>
        private int[] get2Part(int[] toBeSort)
        {
            int len = toBeSort.Length - toBeSort.Length / 2;
           int[] part2 = new int[len];

            Array.Copy(toBeSort, toBeSort.Length / 2, part2, 0, len);
            return part2;
        }

        /// <summary>
        /// Get the first part of the array.
        /// </summary>
        /// <param name="toBeSort">The array to be sort.</param>
        /// <returns>The first part of the array.</returns>
        private int[] get1Part(int[] toBeSort)
        {
            int len = toBeSort.Length / 2;
            int[] part1 = new int[len];
           
            Array.Copy(toBeSort, part1, len);
            return part1;
        }
    }
}