首页 > 编程语言 > 详细

冒泡排序

时间:2021-08-05 10:16:59      阅读:22      评论:0      收藏:0      [点我收藏+]
 1 import java.util.Arrays;
 2 
 3 public class Array04 {
 4     public static void main(String[] args) {
 5         //冒泡排序
 6         int[] arr = {45,23,0,5,2,63,4,3,373,5,8,59,3};
 7         sort(arr);
 8         System.out.println(Arrays.toString(arr));
 9     }
10     public static int[] sort(int[] arr){
11         int temp = 0;
12         for (int i = 0; i < arr.length-1; i++) {
13             boolean flag= false;
14             for (int j = 0; j < arr.length-1; j++) {
15                 if (arr[j]> arr[j+1]){
16                     temp= arr[j];
17                     arr[j]= arr[j+1];
18                     arr[j+1]= temp;
19                     flag= true;
20                 }
21             }
22             if (flag= false){
23                 break;
24             }
25         }
26         return arr;
27     }
28 }

结果

[0, 2, 3, 3, 4, 5, 5, 8, 23, 45, 59, 63, 373]

 

冒泡排序

原文:https://www.cnblogs.com/zkn-bebut/p/15101089.html

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