当前位置:网站首页>升级版的冒泡排序:鸡尾酒排序(快乐小时排序)
升级版的冒泡排序:鸡尾酒排序(快乐小时排序)
2022-08-02 23:31:00 【林慈桥】
- 冒泡排序每一轮比较元素都是从左到右,进行单项的元素交换
- 如果升序排序的最小元素在末尾,且前面的元素已经排序好了,那么我们的外循环则需要遍历n-1次,这样就产生了很多次无用的遍历和元素交换
- 我们可以从左到右遍历交换元素后,再从右到左遍历交换元素,类似于钟摆一样,直到中间元素,就排序完成了
- 鸡尾酒排序的java实现
public class Test { public static void main(String[] args) { int[] a = {2, 11, 10, 3, 9, 2, 6, 4, 5, 3, 2, 1}; cocktailSort(a); System.out.println("sort:" + Arrays.toString(a)); } /** * 鸡尾酒升序排序算法 * * @param array */ public static void cocktailSort(int array[]) { int temp;//交换元素的临时变量 int rightBoundary = array.length - 1;//无序区的右边界 int leftBoundary = 0;无序区的左边界 for (int i = 0; i < array.length / 2; i++) {//外层循环遍历的次数array.length的一半 boolean sorted = true;//有序标记,每一轮都设置为true int rightLastExchange = 0;//最后一次交换元素的右边界位置 for (int j = leftBoundary; j < rightBoundary; j++) {//内层循环遍历相邻元素进行比较和交换 if (array[j] > array[j + 1]) {//相邻的前一个元素大于后一个元素则交换 temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; sorted = false;//有元素交换,设置排序完成为false rightLastExchange = j;//有元素交换,记录下交换的下标 } } rightBoundary = rightLastExchange;//最后一次交换元素的下标就是下次遍历的边界 if (sorted) {//排序完成 break; } sorted = true; int leftLastExchange = 0;//最后一次交换元素的左边界位置 for (int j = rightBoundary; j > leftBoundary; j--) {//内层循环遍历相邻元素进行比较和交换 if (array[j] < array[j - 1]) {//相邻的后一个元素小于前一个元素则交换 temp = array[j]; array[j] = array[j - 1]; array[j - 1] = temp; sorted = false;//有元素交换,设置排序完成为false leftLastExchange = j;//有元素交换,记录下交换的下标 } } leftBoundary = leftLastExchange;//最后一次交换元素的下标就是下次遍历的边界 if (sorted) {//排序完成 break; } } } } - 鸡尾酒算法可以再特定的条件小减少排序的遍历次数,但是实现的代码量却增加了一倍,只有在大部分元素已经有序的情况下能发挥优势
边栏推荐
- 思源笔记 本地存储无使用第三方同步盘,突然打不开文件。
- js基础知识整理之 —— Date和定时器
- Visual Studio中vim模拟器
- Find My技术|智能防丢还得看苹果Find My技术
- 如何快速对接淘宝开放平台API接口(淘宝店铺订单明文接口,淘宝店铺商品上传接口,淘宝店铺订单交易接口)
- Connect the Snowflake of CKAN tutorial CKAN to release to open data portal
- d合并json
- 有奖提问|《新程序员》专访“Apache之父”Brian Behlendorf
- 买母婴产品先来京东“券民空间站”抢券!大牌好物低至5折
- 2022 China Eye Expo, Shandong Eye Health Exhibition, Vision Correction Instrument Exhibition, Eye Care Products Exhibition
猜你喜欢

Technology Sharing | How to do assertion verification for xml format in interface automation testing?

智能电视竞争白热化,利用小程序共建生态突围

MySQL最大建议行数2000w, 靠谱吗?

Visual Studio中vim模拟器

Connect the Snowflake of CKAN tutorial CKAN to release to open data portal

为了面试阿里,熬夜肝完这份软件测试笔记后,Offer终于到手了

IDEA多线程调试

【多线程】线程与进程、以及线程进程的调度

Apache Doris 1.1 特性揭秘:Flink 实时写入如何兼顾高吞吐和低延时

The latest real software test interview questions are shared. Are you afraid that you will not be able to enter the big factory after collecting them?
随机推荐
服务间歇性停顿问题优化|得物技术
Merge two excel spreadsheet tools
买母婴产品先来京东“券民空间站”抢券!大牌好物低至5折
What is the matter that programmers often say "the left hand is knuckled and the right hand is hot"?
NLP commonly used Backbone model cheat sheet (1)
十二、form表单的提交
flutter 每个要注意的点
Numpy数组中d[True]=1的含义
2022 China Eye Expo, Shandong Eye Health Exhibition, Vision Correction Instrument Exhibition, Eye Care Products Exhibition
CIO修炼手册:成功晋升CIO的七个秘诀
Controller层代码这么写,简洁又优雅!
js基础知识整理之 —— Date和定时器
js基础知识整理之 —— 闭包
【系统架构设计师】第三章 数据库系统
九零后程序员心声:互联网的同行们,别卷了,再卷人都卷没了
21天学习挑战赛(1)设备树的由来
记一次sql优化Using temporary; Using filesort
有奖提问|《新程序员》专访“Apache之父”Brian Behlendorf
十年架构五年生活-03作为技术组长的困扰
WAF WebShell Trojan free to kill