码迷,mamicode.com
首页 > 编程语言 > 详细

【2017-02-28】冒泡排序

时间:2017-03-01 23:30:12      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:read   ons   aaa   pre   对比   main   rgs   blog   打印   

冒泡排序就是比大小,若前者大于后者,则两者交换位置。用两个For循环嵌套来实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 数组
{
    class Program
    {
        static void Main(string[] args)
        {
            //冒泡排序 3 5 1 2 4 (12345)
            //思路:第一个for循环执行第一次的时候把第一个数拿出来和后面的数挨个比较,如果第一个数比后面的数大,则二者交换位置。
            //第一个for循环完一次的时候最小的数已经放到了最上面。
            //再进行第一个for循环执行第二次,把第二个数拿出来和后边的数比较,若第二个数比后边的数大,则二者交换位置....
            //以此类推,完成冒泡排序
            int[] a = new int[] { 3, 5, 2, 1, 4 };

            for (int i = 0; i < a.Length - 1; i++)  //把每一个数都拽出来,最后一个数不用和下一个空比较。
            {
                for (int j = i + 1; j < a.Length; j++)  //用来对比的数,从i+1开始
                {
                    if (a[i] > a[j])       //如果拽出来的数比要比较的数大,则两个数交换位置
                    {
                        int f = a[i];      //利用第三者交换两者的值
                        a[i] = a[j];
                        a[j] = f;
                    }
                }
            }

            for (int i = 0; i < a.Length; i++)
            {
                Console.WriteLine(a[i]);
            }

            Console.ReadLine();
        }
    }
}

作业题:

string[] ss = new string[5]{"aaa","a","aa","aaaaa","aaaa"};
从大到小打印出来,从小到大打印出来

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 冒泡排序
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] ss = new string[5] { "aaa", "a", "aa", "aaaaa", "aaaa" };
            

            for (int i = 0; i < ss.Length-1; i++)
            {
                for (int j = i+1; j < ss.Length; j++)
                {
                    if (ss[i].Length > ss[j].Length)
                    {
                        string a = ss[i];
                        ss[i] = ss[j];
                        ss[j] = a;
                    }

                }

            }
            for (int i = 0; i < ss.Length; i++)
            {
                Console.WriteLine(ss[i]);
            }
                Console.ReadLine();

                for (int i = 0; i < ss.Length - 1; i++)
                {
                    for (int j = i + 1; j < ss.Length; j++)
                    {
                        if (ss[i].Length <ss[j].Length)
                        {
                            string a = ss[i];
                            ss[i] = ss[j];
                            ss[j] = a;
                        }

                    }

                }
                for (int i = 0; i < ss.Length; i++)
                {
                    Console.WriteLine(ss[i]);
                }
                Console.ReadLine();

        }
    }
}

 

【2017-02-28】冒泡排序

标签:read   ons   aaa   pre   对比   main   rgs   blog   打印   

原文地址:http://www.cnblogs.com/qq609113043/p/6486626.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!