冒泡排序两个 for 循环分别有什么作用?能用最简单的话解释吗?
回复内容:
本来想上个直观图:发现知乎不支持 gif 格式,给个链接吧http://upload.wikimedia.org/wikipedia/commons/3/37/Bubble_sort_animation.gif
对于「冒泡排序」算法,核心是 冒泡。
如何冒泡?也就是说,把数组中最小的那个往上冒,冒的过程就是和他相邻的元素交换。这个冒的过程就是内循环。
经过了一个冒的过程,可以使一个最小的元素冒出来,如果数组里面有 n 个元素,就得冒 n-1 次,这就是外循环。
附我一篇博文:为什么说任何基于比较的算法将5个元素排序都需要7次? 冒泡就是,经过多轮相邻两两比较交换,每次都把当前数组中最小的“冒”到最头上,然后把这个数排除在外,剩下的那些数字不断重复这个过程,最后形成一个有序的数列。
第一个循环(外循环),负责把那个数字排除在外。
第二个循环(内循环),负责两两比较交换
可以自己随便编造个数组,然后对着随便哪找的代码模拟一遍,然后就理解了。
想想有一群泡泡
其中一个泡泡跑到一个泡小妹说,小妹小妹你过来咱俩比比谁大,小妹说哇你好大,于是他跑到了泡小妹前面,又跟前面的一个泡大哥说,大哥,咱俩比比谁大呗。泡大哥看了他一眼他就老实了。这就是内层的for,那个泡泡跟每个人都比一次。
话说那个泡泡刚老实下来,另一个泡泡又开始跟别人比谁大了,这就是外层的for,每个泡泡都会做一次跟其他泡泡比个没完的事。 第一个循环目的是把你每次需要的泡冒到该次的最上面
第二个循环目的是要冒多少次才能把所有泡冒完。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Alipay PHP...

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
