java异或排序算法_排序算法(一)四种冒泡排序

packagecom.zby.sort;/***@authorzby

* @title BubbleSort

* @date 2019年7月9日

* @description 冒泡排序*/

public classBubbleSort {public static voidmain(String[] args) {

run(10);

run(10000);

run(100000);

}/*** 使用工具类排序

*

*@paramsize*/

private static void run(intsize) {int[] intArrayShiwan =SortUtil.getIntArray(size);

SortUtil.run("冒泡排序(临时变量交换)", intArrayShiwan, BubbleSort::bubbleSortWithTempExchange);

SortUtil.run("冒泡排序(异或交换)", intArrayShiwan, BubbleSort::bubbleSortWithXorExchange);

SortUtil.run("冒泡排序(趟数优化)", intArrayShiwan, BubbleSort::bubbleSortRanksOptimize);

SortUtil.run("冒泡排序(交换优化)", intArrayShiwan, BubbleSort::bubbleSortExchangeOptimize);

}/*** 冒泡排序(临时变量交换)

*

*@paramarr 待排序数组*/

public static void bubbleSortWithTempExchange(int[] arr) {inttemp;for (int i = 0; i < arr.length - 1; i++) {for (int j = 0; j < arr.length - 1 - i; j++) {if (arr[j] > arr[j + 1]) {

temp=arr[j];

arr[j]= arr[j + 1];

arr[j+ 1] =temp;

}

}

}

}/*** 冒泡排序(异或交换)

*

*@paramarr 待排序数组*/

public static void bubbleSortWithXorExchange(int[] arr) {for (int i = 0; i < arr.length - 1; i++) {for (int j = 0; j < arr.length - 1 - i; j++) {if (arr[j] > arr[j + 1]) {

arr[j]= arr[j] ^ arr[j + 1];

arr[j+ 1] = arr[j] ^ arr[j + 1];

arr[j]= arr[j] ^ arr[j + 1];

}

}

}

}/*** 冒泡排序比较趟数优化,但是多了判断、赋值

*

*@paramarr 待排序数组*/@Deprecatedpublic static void bubbleSortRanksOptimize(int[] arr) {inttemp;for (int i = 0; i < arr.length - 1; i++) {boolean ordered = true;for (int j = 0; j < arr.length - 1 - i; j++) {if (arr[j] > arr[j + 1]) {

temp=arr[j];

arr[j]= arr[j + 1];

arr[j+ 1] =temp;

ordered= false;

}

}if(ordered) {break;

}else{

ordered= true;

}

}

}/*** 冒泡排序交换优化(使用冒泡的方式找最大值,使用索引的方式一趟一次交换)

*

*@paramarr 待排序数组*/

public static void bubbleSortExchangeOptimize(int[] arr) {inttemp;intmaxIndex;for (int i = 0; i < arr.length - 1; i++) {int lastIndex = arr.length - i - 1;

maxIndex= 0;for (int j = 0; j <= lastIndex; j++) {if (arr[maxIndex]

maxIndex=j;

}

}if (lastIndex !=maxIndex) {

temp=arr[maxIndex];

arr[maxIndex]=arr[lastIndex];

arr[lastIndex]=temp;

}

}

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值