Comments (1)
哈哈,下面有优化内循环版的,只是用的while,哈哈~
/**
* 优化冒泡排序
*
* @param array $value 待排序数组
* @return array
*/
function bubble_better($value = [])
{
$flag = true; // 标示 排序未完成
$length = count($value)-1; // 数组长度
$index = $length; // 最后一次交换的索引位置 初始值为最后一位
while ($flag) {
$flag = false; // 假设排序已完成
for ($i=0; $i < $index; $i++) {
if ($value[$i] > $value[$i+1]) {
$flag = true; // 如果还有交换发生 则排序未完成
$last = $i; // 记录最后一次发生交换的索引位置
$tmp = $value[$i];
$value[$i] = $value[$i+1];
$value[$i+1] = $tmp;
}
}
$index = $last;
}
return $value;
}
from easy-tips.
Related Issues (20)
- 悲观锁 HOT 8
- English version HOT 1
- 关于设计模式中单例模式的意见 HOT 1
- 有歧义的变量命名 HOT 2
- [英文版翻译申请] HOT 8
- 数据结构 HOT 1
- 感觉之前提过的代码都不见了啊。。 HOT 3
- TIGERB/easy-tips/php/artisan.md 文件中关于使用匿名函数代码错误 HOT 3
- 引擎部分,聚簇索引上下文介绍不一致 HOT 4
- easy-tips/patterns/builder/ 建造者模式有错误,方法名字不对 HOT 2
- 悲观锁执行超时处理 HOT 1
- 设计模式 HOT 1
- 单利模式 HOT 1
- TIGERB------ HOT 1
- 为什么作者的设计模式25种 HOT 3
- 老哥,最近更新很慢啊 HOT 1
- 再通过list生成map时可通过内置函数实现 HOT 1
- tcp/ip网络模型想补充下 HOT 1
- Magic method access test HOT 1
- 组件并发模式会出现 go协程阻塞积压 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from easy-tips.