• C#代码实现-冒泡排序


    冒泡排序原理:(升序)通过当前位置数和后一个位置数进行比较 如果当前数比后一个数大 则交换位置, 完成后 比较基数的位置变成下一个数。直到数组末尾,当程序运行完第一遍 最大的数已经排序到最后一个位置了。次数可以减少循环数不用管最后一个数

    降序排序同理 不过是把比较方式变成判断当前数是否小于下一个数 如果小于则交换

    下面直接上代码

    双重循环方式:

     1 using System;
     2 using System.Collections.Generic;
     3 
     4 namespace TestConsole
     5 {
     6     class Program
     7     {
     8         static void Main(string[] args)
     9         {
    10             //创建一个乱序数组
    11             List<int> ints = new List<int> { 1, 2, 3, 4, 8, 6, 4, 1, 0, 5, 5, 0, 5, 1, 16, 1, 32, 1, 54, 68, 4, 21, 56, 14, 856, 48, 6, 12, 3, 5 };
    12 
    13             //获取数组长度
    14             int count = ints.Count;
    15 
    16             //外圈循环 数组有多少个数就循环多少次  每完成一次内部循环减少一次外部循环(最后一个数以经是最大  不用参与比较了)
    17             for (int i = count; i > 0; i--)
    18             {
    19                 //内部循环  比较当前位置数和下一个位置的数大小
    20                 for (int j = 0; j < i; j++)
    21                 {
    22                     //判断是否到数组尾
    23                     if (j + 1 == i) continue;
    24                     //判断当前数是否比下一个数大
    25                     if (ints[j] > ints[j + 1])
    26                     {
    27                         //把当前数替换到临时变量
    28                         var t = ints[j];
    29                         //把下一个数替换到当前位置
    30                         ints[j] = ints[j + 1];
    31                         //把临时变量替换到下一个数的位置
    32                         ints[j + 1] = t;
    33                     }
    34                 }
    35                 //减少外圈循环
    36                 count--;
    37             }
    38             Console.WriteLine(string.Join(",", ints)/*string.Join("分隔符",对象数组)  用于把数组元素分割成字符串*/ );
    39             Console.ReadKey();
    40         } 
    41     }
    42 }

    while实现方式:

     1 using System;
     2 using System.Collections.Generic;
     3 
     4 namespace TestConsole
     5 {
     6     class Program
     7     {
     8         static void Main(string[] args)
     9         { 
    10             //创建一个乱序数组
    11             List<int> ints = new List<int> { 1, 2, 3, 4, 8, 6, 4, 1, 0, 5, 5, 0, 5, 1, 16, 1, 32, 1, 54, 68, 4, 21, 56, 14, 856, 48, 6, 12, 3, 5 };
    12 
    13             //获取数组长度
    14             int count = ints.Count;
    15 
    16             //外圈循环 数组有多少个数就循环多少次  每完成一次内部循环减少一次外部循环(最后一个数以经是最大  不用参与比较了)
    17             while (count > 0)
    18             {
    19                 //内部循环  比较当前位置数和下一个位置的数大小
    20                 for (int j = 0; j < count; j++)
    21                 {
    22                     //判断是否到数组尾
    23                     if (j + 1 == count) continue;
    24                     //判断当前数是否比下一个数大
    25                     if (ints[j] > ints[j + 1])
    26                     {
    27                         //把当前数替换到临时变量
    28                         var t = ints[j];
    29                         //把下一个数替换到当前位置
    30                         ints[j] = ints[j + 1];
    31                         //把临时变量替换到下一个数的位置
    32                         ints[j + 1] = t;
    33                     }
    34                 }
    35                 //减少外圈循环
    36                 count--;
    37             } 
    38             Console.WriteLine(string.Join(",", ints)/*string.Join("分隔符",对象数组)  用于把数组元素分割成字符串*/ );
    39             Console.ReadKey();
    40         }
    41     }
    42 }

    纯属个人理解,如果偏差请各位大佬指正~~~~

  • 相关阅读:
    MingW 综合资料参考
    技术文档编写的参考
    web地图的几个参考地址
    Linux的版本
    学习C语言一些的好的书和网站
    基于JAVA的web框架 GWT SmartGWT ExtGWT Vaadin
    Linux Shell的类别
    JavaScript图书推荐
    云盘分享 自绘画【儿童眼里的世界的确不同】
    GWT概述
  • 原文地址:https://www.cnblogs.com/colorchild/p/12717386.html
Copyright © 2020-2023  润新知