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++;
                }
            }

  • 相关阅读:
    ORACLE DBA的职责
    oracle开发常用LOV
    Oracle Patch 版本的查询
    指定二次分配为主要分配
    系统日期格式引起的错误:出生日期不能为将来日期
    分享一个帮助你检测网站颜色对比度的在线web工具 checkmycolours
    最常用的CURL命令大全
    超棒的javascript移动触摸设备开发类库 QUOjs
    分享一个超炫HTML5开发的jQuery进度条插件 percentageloader
    纯CSS实现的3D简洁按钮设计
  • 原文地址:https://www.cnblogs.com/liancs/p/3879361.html
Copyright © 2011-2022 走看看