`

java算法----冒泡排序

    博客分类:
  • java
 
阅读更多
package com.zhonghaiwangluokeji.interview;

/**
 * 实现冒泡排序
 * @author yangjianzhou
 *
 */
public class Problem1 {
	
	public static void main(String[] args) {
		int [] xx = {8,4,2,6,3,9,10,2,34,4};
		xx = bubSort(xx);
		for(int i=0;i<xx.length;i++){
			System.out.print(xx[i]+" ");
		}
	}

	public static int[] bubSort(int[] a){
		int temp;
		int len = a.length;
		for(int i=0;i<len;i++){
			for(int j=len-1;j>i;j--){
				if(a[j]<a[j-1]){
					temp = a[j-1];
					a[j-1] = a[j];
					a[j] = temp;
				}
			}
		}
		return a;
	}
}



运行结果:
2 2 3 4 4 6 8 9 10 34 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics