经典排序算法——冒泡排序

 1 public class BubbleSort {
 2     public int[] bubbleSort(int[] arr, int n){
 3         int temp = 0;
 4         
 5         for (int i=0; i<n-1; i++){
 6             for (int j=0; j<n-1; j++){
 7                 if (arr[j] > arr[j+1]){
 8                     temp  = arr[j];
 9                     arr[j] = arr[j+1];
10                     arr[j+1] = temp;
11                 }
12             }
13         }
14         return arr;
15     }
16     
17     public static void main(String[] args){
18         int[] a  = {3, 4, 8, 6, 1, 0, 2};
19         BubbleSort bs = new BubbleSort();
20         bs.bubbleSort(a, a.length);
21         for (int i=0; i<a.length; i++){
22             System.out.println(a[i]);
23         }
24     }
25 }

 

posted @ 2017-10-24 09:37  四季万花筒  阅读(256)  评论(0编辑  收藏  举报