zoukankan      html  css  js  c++  java
  • 几种排序算法

    //冒泡排序法
    public void Sort(int[] source)
            {
                Console.WriteLine("冒泡算法开始......");
                Auxiliary.DisplayArray(source);

                int j, temp;
                j = 1;
                while ((j < source.Length))
                {
                    for (int i = 0; i < source.Length - j; i++)
                    {    
                        if (source[i] < source[i + 1])
                        {
                            temp = source[i];
                            source[i] = source[i + 1];
                            source[i + 1] = temp;
                        }                                       
                    }
    //希尔排序法

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

                } 

    //插入排序算法

     public void Sort(int[] source)
            {

                for (int i = 1; i < source.Length; i++)
                {
                    int t = source[i];
                    int j = i;
                    while ((j > 0) && (source[j - 1] < t))
                    {
                        source[j] = source[j - 1];
                        --j;
                    }
                    source[j] = t;
                }
       }
    //选择排序算法

    public void Sort(int[] source)
            {
                int min;
                int count = 1;
                Console.WriteLine("选择算法开始......");
                Auxiliary.DisplayArray(source);
                for (int i = 0; i < source.Length - 1; i++)
                {
                    min = i;
                    for (int j = i + 1; j < source.Length; j++)
                    {
                        if (source[j] > source[min])
                            min = j;
                    }
                    int t = source[min];
                    source[min] = source[i];
                    source[i] = t;

                    Console.Write("第{0}趟排序:", count);
                    Auxiliary.DisplayArray(source);

                    count++;
                }
            }

  • 相关阅读:
    NodeJS3-1基础API----Path(路径)
    NodeJS2-6环境&调试----debug
    NodeJS2-5环境&调试----process(进程)
    NodeJS2-4环境&调试----global变量
    NodeJS2-3环境&调试----module.exports与exports的区别
    短视频秒播优化实践(二)
    短视频秒播优化实践(一)
    仿抖音上下滑动播放视频
    带着问题,再读ijkplayer源码
    上班一个月,后悔当初着急入职的选择了
  • 原文地址:https://www.cnblogs.com/liancs/p/3879361.html
Copyright © 2011-2022 走看看