zoukankan      html  css  js  c++  java
  • Console算法冒泡排序法|倒水法

    ylbtech-Arithmetic:Console-算法-冒泡排序法|倒水法
     
    1.A,案例

    -- ========================================================
    -- ylb:算法
    -- type:冒泡法排序
    -- thankyou:sunshine, 谢谢你的默默付出
    -- 10:50 2012-04-06
    -- ========================================================

    1.B,解决方案

     1.B.1,算法一

    using System;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            /// <summary>
            /// ylb:冒泡法排序(倒水法)
            /// </summary>
            /// <param name="args"></param>
            static void Main(string[] args)
            {
                int[] array = { 12, 1, 5, 20, 4 };
    
                int temp = 0;
                for (int i = 0; i < array.Length; i++)
                {
    
                    for (int j = i + 1; j < array.Length; j++)
                    {
    
                        //升序
                        if (array[i] > array[j])
                        {
                            temp = array[i];
                            array[i] = array[j];
                            array[j] = temp;
                        }
                    }
                }
    
                //输出排序后的结果
                for (int i = 0; i < array.Length; i++)
                {
    
                    Console.Write(array[i]+"\t");
                }
                Console.WriteLine("\n");
    
                foreach (int item in array)
                {
    
                    Console.Write(item + "\t");
                }
    
            }
        }
    }

    1.B.2,算法二 【操作对象内部储存变量】

    using System;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            public static void Sort(int[] myArray)
            {
    
                // 取长度最长的词组 -- 冒泡法
                for (int j = 1; j < myArray.Length; j++)
                {
                    for (int i = 0; i < myArray.Length - 1; i++)
                    {
                        // 如果 myArray[i] > myArray[i+1] ,则 myArray[i] 上浮一位
                        if (myArray[i] > myArray[i + 1])
                        {
                            int temp = myArray[i];
                            myArray[i] = myArray[i + 1];
                            myArray[i + 1] = temp;
                        }
                    }
                }
            }
            static void Main(string[] args)
            {
                int[] myArray = new int[] { 20, 10, 8, 30, 5, 1, 2, 22 };
                
                Sort(myArray);
    
                for (int m = 0; m < myArray.Length; m++)
                {
                    Console.WriteLine(myArray[m]);
                }
            }
        }
    }
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    鲍尔默称微软将投入数十亿美元打造数据中心业务(转)
    程序流程的辅助控制
    获取程序所在目录
    越界赋值
    长字串与宽字串
    长字串与短字串
    指针的运算
    Windows API 的数据类型与 Delphi 数据类型对照表
    给动态数组添加一个元素
    Delphi 与 C/C++ 数据类型对照表
  • 原文地址:https://www.cnblogs.com/ylbtech/p/2922176.html
Copyright © 2011-2022 走看看