码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-10-24 11:17:14      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:[]   print   code   i++   sys   out   new   temp   ring   

 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 }

 

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

标签:[]   print   code   i++   sys   out   new   temp   ring   

原文地址:http://www.cnblogs.com/slhs/p/7721979.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!