java冒泡排序 快速排序_快速排序冒泡排序比较

//快速排序

public class QuickSort {

public static void quickSort(int[] a,int low,int high){

int mid;

int ltemp,rtemp;

ltemp=low;rtemp=high;

mid=a[(low+high)>>>1];

while(ltemp

while(a[ltemp]

ltemp++;

}

while(a[rtemp]>mid){

rtemp--;

}

if(ltemp

a[ltemp]=a[ltemp]^a[rtemp];

a[rtemp]=a[ltemp]^a[rtemp];

a[ltemp]=a[ltemp]^a[rtemp];

ltemp++;rtemp--;

}

}

if(ltemp==rtemp){

ltemp++;

}

if(ltemp

if(low

}

}

//冒泡排序

public class Sort {

public static void sort(int[] a){

int j=0;

for(int i=1;i

j=i-1;

while(j>=0&&a[j+1]

a[j]=a[j+1]^a[j];a[j+1]=a[j+1]^a[j];a[j]=a[j+1]^a[j];//交换

j--;

}

}

}

}

//测试

class MyTimer{//计时方法。

private final long start;

public MyTimer(){

start=System.currentTimeMillis();

}

public long getElapsed(){

return System.currentTimeMillis()-start;

}

}

public class Test {

//测试方法

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

MyTimer t1=new MyTimer();

for(int i=0;i<100;i++){//测试快速排序速度

int[]  a = new int[1000];

for(int j=0;j

a[j]=(int) (Math.random()*100);

}

QuickSort.quickSort(a,0,a.length-1);

}

System.out.println(t1.getElapsed());

t1=new MyTimer();

for(int i=0;i<100;i++){//测试冒泡排序速度

int[]  a = new int[1000];

for(int j=0;j

a[j]=(int) (Math.random()*100);

}

Sort.sort(a);

}

System.out.println(t1.getElapsed());

t1=new MyTimer();

for(int i=0;i<100;i++){//测试系统内快速排序速度

int[]  a = new int[1000];

for(int j=0;j

a[j]=(int) (Math.random()*100);

}

java.util.Arrays.sort(a);

}

System.out.println(t1.getElapsed());

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值