首页 > 编程语言 > 详细

冒泡排序

时间:2019-09-02 14:35:16      阅读:55      评论:0      收藏:0      [点我收藏+]

冒泡排序算法


public class BubbleSort {
public void bubbleSort(Integer[] arr) {
for (int i = 0; i < arr.length; i++) {
boolean flag = false;
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp;
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
flag = true;
}
}
if (!flag) break;
}
}


public static void main(String[] args) {
Integer arr[] = {2, 4, 7, 6, 8, 5, 9};
BubbleSort bubbleSort = new BubbleSort();
bubbleSort.bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
}

冒泡排序

原文:https://www.cnblogs.com/rocdata/p/11445628.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!