冒泡排序(升序)

 1 /**
 2  * @author: yekai  <br/>
 3  * Date: 2021/11/16:0:04  <br/>
 4  * Description:4 6 5 2 1 3 使用冒泡排序 顺序排列
 5  */
 6 public class Main {
 7     public static void main(String[] args) {
 8         int[] a = { 4,6,5,2,1,3};
 9         int[] b = new int[6];
10         b = sort(a);
11         //输出
12         for (int i = 0; i < b.length; i++) {
13             System.out.print(b[i] + " ");
14         }
15 
16     }
17 
18     //冒泡排序算法
19     private static int[] sort(int[] a) {
20 
21         for (int i = 0; i < a.length; i++) {
22 
23             for (int j = 0; j < a.length-i-1; j++) {
24                 //如果大于就互换
25                 if(a[j]>a[j+1]){
26                     int temp = a[j];
27                     a[j] = a[j+1];
28                     a[j+1] = temp;
29                 }
30             }
31         }
32         return a;
33     }
34 
35 }

 

posted @ 2021-11-16 14:00  yekaiIT  阅读(80)  评论(0编辑  收藏  举报