• C# 常见排序算法


     //冒泡排序算法
            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;
            }
  • 相关阅读:
    项目中openlayer中使用,完整解决方案(数据库矢量数据,动态更新,分层,编辑)
    openlayer
    关于splitViewController自己的总结....
    GIS底层开发总结
    判断联网 phone
    nsdate 前一天 后一天
    ObjectiveC 字符处理函数 全 substring indexof
    oracle
    Windows Xp上跑IIS5.1x 用户访问过多出错
    Jquery中替换节点的方法replaceWith()和replaceAll()
  • 原文地址:https://www.cnblogs.com/lihfeiblogs/p/4123051.html
Copyright © 2020-2023  润新知