<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>实现 on 庞玉栋个人博客</title><link>https://pangyd.com/tags/%E5%AE%9E%E7%8E%B0/</link><description>Recent content in 实现 on 庞玉栋个人博客</description><generator>Hugo</generator><language>zh-cn</language><lastBuildDate>Thu, 28 Mar 2019 09:32:53 +0800</lastBuildDate><atom:link href="https://pangyd.com/tags/%E5%AE%9E%E7%8E%B0/index.xml" rel="self" type="application/rss+xml"/><item><title>Java实现冒泡排序</title><link>https://pangyd.com/post/211-java/</link><pubDate>Sat, 16 Feb 2019 10:38:10 +0800</pubDate><guid>https://pangyd.com/post/211-java/</guid><description>&lt;p>&lt;strong>冒泡排序：&lt;/strong>&lt;/p>&lt;p>利用冒泡排序对数组进行排序&lt;/p>&lt;p>&lt;strong>基本概念：&lt;/strong>&lt;/p>&lt;p>依次比较相邻的两个数，将小数放在前面，大数放在后面。即在第一趟：首先比较第1个和第2个数，将小数放前，大数放后。然后比较第2个数和第3个数，将小数放前，大数放后，如此继续，直至比较最后两个数，将小数放前，大数放后。至此第一趟结束，将最大的数放到了最后。在第二趟：仍从第一对数开始比较（因为可能由于第2个数和第3个数的交换，使得第1个数不再小于第2个数），将小数放前，大数放后，一直比较到倒数第二个数（倒数第一的位置上已经是最大的），第二趟结束，在倒数第二的位置上得到一个新的最大数（其实在整个数列中是第二大的数）。如此下去，重复以上过程，直至最终完成排序。&lt;/p>&lt;p>&lt;b>代码如下：&lt;/b>&lt;/p>&lt;pre lay-lang="Java" class="layui-code" skin="notepad">&lt;p>&lt;b>package 冒泡排序;
import java.util.Arrays;
&lt;p>public class BubbleSort1 {
public static void BubbleSort(int[] arr) {
boolean flag = true;
while(flag){
int temp;//定义一个临时变量
for(int i=0;i&amp;lt;arr.length-1;i++){//冒泡趟数，n-1趟
for(int j=0;j&amp;lt;arr.length-i-1;j++){
if(arr[j+1]&amp;lt;arr[j]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
flag = true;
}
}
if(!flag){
break;//若果没有发生交换，则退出循环
}
}
}
}
public static void main(String[] args) {
int arr[] = new int[]{1,6,2,2,5};
BubbleSort.BubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
}&lt;/b>&lt;/p>&lt;/pre>&lt;p>&lt;/p>&lt;/p></description></item></channel></rss>