java冒泡排序(最精简代码)
2012-09-29 00:00
288 查看
public class BubbleSort { public void bubbleSort(int[] array) { int temp; boolean flag = true; for (int i = 0; i < array.length - 1 && flag == true; i++) {// 比较时为i<array.length-1,因为有两个数在比较 flag = false; for (int j = 0; j < array.length - i - 1; j++) { if (array[j] > array[j + 1]) {// 如果要从大到小排序只需改变此处顺序即可 temp = array[j + 1]; array[j + 1] = array[j]; array[j] = temp; flag = true; } } // 打印每趟排序结果 for (int m = 0; m <= array.length - 1; m++) {// 输出时为i<=array.length-1 System.out.print(array[m] + "\t"); } System.out.println(); } } public static void main(String[] args) { BubbleSort bubbleSort = new BubbleSort(); int[] array = { 5, 69, 12, 3, 56, 789, 2, 5648, 23 }; bubbleSort.bubbleSort(array); System.out.println("排序后结果为:"); for (int i = 0; i <= array.length - 1; i++) {// 输出时为i<=array.length-1 System.out.print(array[i] + "\t"); } } }
相关文章推荐
- Lombok精简Java代码及安装使用
- Java冒泡排序代码详解
- 代码经过精简后的仿QQ折叠菜单-滑动门选项卡
- 及其精简的一段代码对IE6的判断
- x264代码精简优化
- 使您的CSS网页布局代码更专业 精简CSS文件
- C#/Net代码精简优化技巧
- Silverlight+WCF双工通信开发聊天室精简版服务器端代码
- 矩阵按列按行归一化到L2范数的原理和最精简Matlab代码
- javascript之学会吝啬 精简代码
- java归并排序(最精简代码)
- POST 传多值 精简代码
- Python 代码精简和优化
- 0013冒泡法排序之指针实现篇(非常精简的代码)
- 宽高比例不固定的用户头像纵向居中的方法(不使用表格、代码精简、图片不变形)
- 精简代码:Python的split方法函数可以分割字符串成列表,默认是以空格作为分隔符sep来分割字符串。
- 14. Longest Common Prefix 简单的题,但是有精简代码的小技巧
- 二进制中1的个数(最精简代码)
- \t\tJS短路原理的应用 精简代码的途径
- 精简ZBlog模版代码,提高页面打开速度