zoukankan      html  css  js  c++  java
  • 【2017-02-28】冒泡排序

    冒泡排序就是比大小,若前者大于后者,则两者交换位置。用两个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();
    
            }
        }
    }
  • 相关阅读:
    SQLLoader7(只导入数据文件的其中几行记录)
    SQLLoader6(一个或多个数据文件按条件导入不同的表)
    SQLLoader5(从多个数据文件导入到同一张表)
    SQLLoader4(数据文件中的列与表中列不一致情况-filler)
    SQLLoader3(数据文件没有分隔符时的导入)
    SQLLoader2(导入EXCEL或csv格式的文件)
    SQLLoader1(简单测试,以控制文件方式导入数据)
    可编程内存
    JSONP
    SSL协议
  • 原文地址:https://www.cnblogs.com/qq609113043/p/6486626.html
Copyright © 2011-2022 走看看