码迷,mamicode.com
首页 > 其他好文 > 详细

正宗的冒泡排序

时间:2014-08-19 14:46:25      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:style   for   ar   new   size   on   sp   c   

冒泡排序是两两比较相邻记录关键字,如果反序则交换。很奇怪,总有人写错,比如我寝室的zdc,曾几何时,他还很得意地和我说写出了冒泡~~

package my_algorithm;

public class MyBubbleSort {

public static void swap(int[] a,int i, int j)

{

int tmp = a[i];

a[i] = a[j];

a[j] = tmp;

}

public void bubbleSort(int[] a)

{

for(int i = 1;i < a.length; i ++)

for(int j = a.length-2;j >= i; j --)

{

if(a[j] > a[j+1])

swap(a,j,j+1);

}

}

// a[]的第一位a[0]用来存储些其他额外信息

public static void main(String[] args)

{

int[] a = new int[]{6,4,6,2,7,12,3};

MyBubbleSort mbs = new MyBubbleSort();

mbs.bubbleSort(a);

for(int i = 0; i < a.length; i++)

System.out.print(a[i] + " ");

}

}


至此完毕~~

正宗的冒泡排序,布布扣,bubuko.com

正宗的冒泡排序

标签:style   for   ar   new   size   on   sp   c   

原文地址:http://my.oschina.net/DanielLee/blog/304257

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