码迷,mamicode.com
首页 > 编程语言 > 详细

C# 常见排序算法

时间:2014-11-26 15:50:19      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   color   sp   for   on   div   log   

 //冒泡排序算法
        static void Main(string[] args)
        {
            int[] array = { 2, 3, 7, 1, 4, 9, 6, 8, 10 };
            var newarrry = BubbleSort(array);
            for (int i = 0; i < newarrry.Length; i++)
            {
                Console.WriteLine(newarrry[i]);
            }
            Console.Read();
        }

        public static int[] BubbleSort(int[] array)
        {
            int temp = 0;
            for (int i = 0; i < array.Length - 1; i++)
            {
                for (int j = i + 1; j < array.Length; j++)
                {
                    //if (array[j] < array[i])//冒泡升序
                    //{
                    //    temp = array[i];
                    //    array[i] = array[j];
                    //    array[j] = temp;
                    //}

                    if (array[j] > array[i])//冒泡降序
                    {
                        temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
            return array;
        }

        //选择排序算法
        static void Main(string[] args)
        {
            int[] array = { 2, 3, 7, 1, 4, 9, 6, 8, 10 };
            var newarrry = SelectSort(array);
            for (int i = 0; i < newarrry.Length; i++)
            {
                Console.WriteLine(newarrry[i]);
            }
            Console.Read();
        }

        public static int[] SelectSort(int[] array)
        {
            int temp = 0;
            int minindex = 0;
            for (int i = 0; i < array.Length - 1; i++)
            {
                minindex = i;
                for (int j = i; j < array.Length; j++)
                {
                    if (array[j] < array[minindex])//每次循环选择最小的数插入到数组中
                    {
                        minindex = j;
                    }
                }
                temp = array[minindex];
                array[minindex] = array[i];
                array[i] = temp;
            }
            return array;
        }

        //插入排序算法
        static void Main(string[] args)
        {
            int[] array = { 2, 3, 7, 1, 4, 9, 6, 8, 10 };
            var newarrry = InsertSort(array);
            for (int i = 0; i < newarrry.Length; i++)
            {
                Console.WriteLine(newarrry[i]);
            }
            Console.Read();
        }

        public static int[] InsertSort(int[] array)
        {

            for (int i = 1; i < array.Length; i++)
            {
                int t = array[i];
                int j = i;
                while ((j > 0) && (array[j - 1] > t))
                {
                    array[j] = array[j - 1];
                    j--;
                }
                array[j] = t;
            }
            return array;
        }


        //希尔排序算法
        static void Main(string[] args)
        {
            int[] array = { 2, 3, 7, 1, 4, 9, 6, 8, 10 };
            var newarrry = ShellSort(array);
            for (int i = 0; i < newarrry.Length; i++)
            {
                Console.WriteLine(newarrry[i]);
            }
            Console.Read();
        }

        public static int[] ShellSort(int[] array)
        {
            int inc;
            for (inc = 1; inc <= array.Length / 9; inc = 3 * inc + 1) ;
            for (; inc > 0; inc /= 3)
            {
                for (int i = inc + 1; i <= array.Length; i += inc)
                {
                    int t = array[i - 1];
                    int j = i;
                    while ((j > inc) && (array[j - inc - 1] > t))
                    {
                        array[j - 1] = array[j - inc - 1];
                        j -= inc;
                    }
                    array[j - 1] = t;
                }
            }
            return array;
        }

 

C# 常见排序算法

标签:style   blog   ar   color   sp   for   on   div   log   

原文地址:http://www.cnblogs.com/lihfeiblogs/p/4123051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!