• 几种排序算法


    //冒泡排序法
    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++;
                }
            }

  • 相关阅读:
    【EOSS】Windows 环境编译 Apollo 代码
    【配置与安装】CentOS7 升级gcc版本
    【Linux系统】Linux 环境变量 PATH 的增添和删除
    【C++百科】std(标准库)与 stl(标准模板库)的关系
    【C++语法】C ++ 函数后面加 throw() 的作用
    【gtest/gmock】gtest:Google C++ Testing Framework
    【Linux文件系统】inode(索引节点)和 block(块)
    【Linux命令】ls -l显示的内容中total到底是什么?
    【vi】Linux vi/vim 简介
    【DOS命令】color
  • 原文地址:https://www.cnblogs.com/liancs/p/3879361.html
Copyright © 2020-2023  润新知