java人物跳舞的算法_结合舞蹈更易理解的算法--冒泡排序算法[java代码]

舞蹈地址:http://t.cn/hrf58M

冒泡排序算法java代码:

/**

* 冒泡排序

* @author hsy

*

*/

public class BubbleSort

{

private long[] a;

private int nElems;

public BubbleSort( int max)

{

super();

this.a = new long[max];

nElems = 0;

}

/**

* 向数组中插入数据

* @param value

*/

public void insert(long value){

a[nElems] = value;

nElems++;

}

/**

* 打印数组

*

*/

public void display(){

for(int i=0;i

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

}

System.out.println("");

}

/**

* 排序

*

*/

public void bubbleSort(){

int out;

int in;

for(out=nElems-1;out>1;out--){

for(in=0;in

if(a[in]>a[in+1]){

swap(in,in+1);

}

}

}

}

/**

* 大小换位

* @param index1

* @param index2

*/

private void swap(int index1,int index2){

long temp = a[index1];

a[index1] = a[index2];

a[index2] = temp;

}

public static void main(String[] args){

int max = 100;

BubbleSort arr = new BubbleSort(max);

arr.insert(10);

arr.insert(5);

arr.insert(45);

arr.insert(8);

arr.insert(34);

arr.insert(78);

arr.insert(22);

arr.insert(68);

arr.insert(82);

arr.insert(53);

arr.insert(7);

arr.insert(99);

arr.insert(57);

arr.insert(72);

arr.display();

long start = System.currentTimeMillis();

arr.bubbleSort();

long end = System.currentTimeMillis();

System.out.println("排序耗时:"+(end-start));

arr.display();

}

}

控制台输出:

10,5,45,8,34,78,22,68,82,53,7,99,57,72,

排序耗时:0

5,7,8,10,22,34,45,53,57,68,72,78,82,99,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值