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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    CSS3 box-shadow(阴影使用)
    缩小窗口时CSS背景图出现右侧空白BUG的解决方法
    html打开个人QQ聊天页面
    帮助你实现元素动画的6款插件
    给出两个颜色,计算中间颜色返回数组
    名字首字母
    自适应网页设计
    tab切换jquery代码
    改变上传文件样式
    剑指offer 面试16题
  • 原文地址:https://www.cnblogs.com/ylbtech/p/2922176.html
Copyright © 2011-2022 走看看