zoukankan      html  css  js  c++  java
  • 冒泡排序——算法

    计算机经典算法 ——冒泡排序(降序排序)

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace SortTest
     7 {
     8     class Program
     9     {
    10         /*
    11          * 降序排列
    12          */
    13         static void Sort(int[] iNum)
    14         {
    15             for (int i = 0; i < iNum.Length;i++ )
    16             {
    17                 for (int j=i+1;j<iNum.Length;j++)
    18                 {
    19                     int temp =0;
    20                     if (iNum[i]<iNum[j])
    21                     {
    22                         temp = iNum[i];
    23                         iNum[i] = iNum[j];
    24                         iNum[j] = temp;
    25                     }
    26                 }
    27             }
    28         }
    29         static void PrintNumber(int[] iNums)
    30         {
    31             Console.Write("数组的顺序: ");
    32             foreach (var iNum in iNums)
    33             {
    34                 Console.Write(iNum+ " ");
    35             }
    36             Console.WriteLine();
    37         }
    38         static void Main(string[] args)
    39         {
    40             int[] iNumber = { 9, 6, 7, 8};
    41             PrintNumber(iNumber);
    42             Sort(iNumber);
    43             PrintNumber(iNumber);
    44             Console.ReadKey();
    45         }
    46     }
    47 }

     2.冒泡排序(升序排序)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace AscendingSort
    {
        class Program
        {
            /************************************************************************/
            /* 升序排列                                                             */
            /************************************************************************/
            static void AscSort(int[] iNum)
            {
                for (int i = 0; i < iNum.Length;i++ )
                {
                    for (int j = iNum.Length - 1; j > i; j--)
                    {
                        if (iNum[i]>iNum[j])
                        {
                            int temp = 0;
                            temp = iNum[i];
                            iNum[i] = iNum[j];
                            iNum[j] = temp;
                        }
                    }
                }
            }
            static void PrintNumber(int[] iNums)
            {
                Console.Write("数组的顺序: ");
                foreach (var iNum in iNums)
                {
                    Console.Write(iNum + " ");
                }
                Console.WriteLine();
            }
            static void Main(string[] args)
            {
                int[] iNumber = { 9, 6, 7, 8 };
                PrintNumber(iNumber);
                AscSort(iNumber);
                PrintNumber(iNumber);
                Console.ReadKey();
            }
        }
    }

     另一种升序排序写法

    /// <summary>
            /// 升序排列
            /// </summary>
            /// <param name="iNum"></param>
            static void Bubble(int[] iNum)
            {
                int temp = 0;
                for (int i = iNum.Length; i > 0; i--)
                {
                    for (int j = 0; j < i - 1; j++)
                    {
                        if (iNum[j] > iNum[j + 1])
                        {
                            temp = iNum[j];
                            iNum[j] = iNum[j + 1];
                            iNum[j + 1] = temp;
                        }
                    }
                }
            }
  • 相关阅读:
    app令牌的一个token实现
    velocity分页模板
    js基础-表单验证和提交
    做项目中没经验遇到的各种问题
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    oracle创建用户
    oracle创建表相关
    pe创建激活administrator后消除问题,删除用户问题
    spring学习遇到的问题汇总
    .NET MVC自定义错误处理页面的方法
  • 原文地址:https://www.cnblogs.com/gylhaut/p/5644802.html
Copyright © 2011-2022 走看看