惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 猫猫

SignalR 2.0 初次使用说明 解决VS2012上面EF字段说明备注没有的方法 生产百万级随机数 (转帖)C#批量重命名文件代码的实现 MyClipse DataBase Explorer 连接 ACCESS 配置说明 一个文本处理小工具(原创) 实现数组转换为DataTable TreeView 部署到服务器上无法显示图标(失效) 仿google的suggest C# 把网页内容转为EXCEL,WORD C# 生产新闻文章分页 C# 实现页面3秒后跳转 ASPNET动态生成静态页面 正则表达式30分钟入门教程 转 正则表达式之道 转 正则表达式基础知识 转 常用正则表式 转 转 正则表达式教程 正则表达式教程 (转)
C# 算法之 冒泡排序
猫猫 · 2008-11-01 · via 博客园 - 猫猫

static void Main(string[] args)
        {
int[] arr ={2,5,8,9,1,57,8,5877,5,58,56,3,557,7};for (int i = 0; i < arr.Length; i++)
            {
                
for (int j = 0; j < arr.Length - i-1; j++)
                {
                    
if (arr[j] > arr[j+1])
                    {
                        
int m=arr[j];
                        arr[j] 
= arr[j+1];
                        arr[j
+1= m;
                    }
                    
                
                }

                           
            }

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

            Console.ReadKey();

        }

现在来讲一下程序运行过程:

2,5,8,1,9,8,57,5,58,56,3,557,7,5877,|  i的第一次循环
2,5,1,8,8,9,5,57,56,3,58,7,557,5877,|  依次
2,1,5,8,8,5,9,56,3,57,7,58,557,5877,|
1,2,5,8,5,8,9,3,56,7,57,58,557,5877,|
1,2,5,5,8,8,3,9,7,56,57,58,557,5877,|
1,2,5,5,8,3,8,7,9,56,57,58,557,5877,|
1,2,5,5,3,8,7,8,9,56,57,58,557,5877,|
1,2,5,3,5,7,8,8,9,56,57,58,557,5877,|
1,2,3,5,5,7,8,8,9,56,57,58,557,5877,|
1,2,3,5,5,7,8,8,9,56,57,58,557,5877,|
1,2,3,5,5,7,8,8,9,56,57,58,557,5877,|
1,2,3,5,5,7,8,8,9,56,57,58,557,5877,|
1,2,3,5,5,7,8,8,9,56,57,58,557,5877,|
1,2,3,5,5,7,8,8,9,56,57,58,557,5877,|

第一次循环的时候:首先arr[0]跟arr[1]比较 ,如果arr[0]大于arr[1]那么原来 arr[0]的值和arr[1]的值互换,再次内循环

arr[1]此时值不变=5 ,arr[2]=8 这两个比较 arr[1]<arr[8]那么不变动, 然后再比较arr[2]=8和arr[3]=1 ,arr[2]>arr[3],值互换,在循环 arr[3]此时值为=8,arr[4]=9, 一次比较

因此第一次循环之后 最后一值肯定是最大的那个值

1,2,5,8,5,8,9,3,56,7,57,58,557,5877,|
此时最大值已经得到,因此下次循环只用比较最大值之前那几位数字!

所以

 for (int j = 0; j < arr.Length - i-1; j++)

需要这样比较!i=0的时候 需要全部比较

i=1的时候只要比较13个数字就可以了(因为最后一个已经是最大的那个值了)

这样就是把最大的那个依次往后排(就像泡泡一样一个一个浮出来)

完整代码:

sing System;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace ConsoleApplication1
{
    
class Program
    {
        
static void Main(string[] args)
        {
int[] arr ={2,5,8,9,1,57,8,5877,5,58,56,3,557,7};for (int i = 0; i < arr.Length; i++)
            {
                
for (int j = 0; j < arr.Length - i-1; j++)
                {
                    
if (arr[j] > arr[j+1])
                    {
                        
int m=arr[j];
                        arr[j] 
= arr[j+1];
                        arr[j
+1= m;
                    }
                    
                
                }
//注意这里只是用来 将每次循环的结果显示出来
                  
//正式里面不用写这个
                for (int l = 0; l < arr.Length; l++)
                {
                    Console.Write(arr[l].ToString() 
+ ",");
                    

                  
                }
                Console.WriteLine(

"|");

                Console.ReadKey();

//到这里
            }for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i].ToString());
            
            }

            Console.ReadKey();

        }

    }
}