PHP中的冒泡排序实现? [英] Bubble sort implementation in PHP?

查看:105
本文介绍了PHP中的冒泡排序实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在PHP中执行冒泡排序算法.

I need to do a bubble sort algorithm in PHP.

我想知道是否有人可以使用任何好的示例,或者开放源代码库可以做到这一点.

I want to know whether any one has any good examples that I can use, or an open source library which can do this.

我在一个集合(数组)中有几个空格,我想用对象(一个人)填充这些空格,所以没有空格可以有一个男性和一个女性,这就是为什么我要找出气泡排序的原因算法.

I have a few spaces in a set (array), i want to fill these spaces with object (a person), so no space can have a male and a female, this why i am trying to find out a bubble sort algorithm.

我的计划是填写任何可用的空格,而不考虑性别,然后分别对它们进行排序.

My plan is to fill in any of the available spaces regardless of the gender, and after that sort them separately.

谢谢.

推荐答案

使用冒泡排序是一个非常糟糕的主意.它的复杂度为O(n^2).

Using bubble sort is a very bad idea. It has complexity of O(n^2).

您应该使用php usort ,它实际上是一种合并排序实现,并保证了O(n*log(n))的复杂性.

You should use php usort, which is actually a merge sort implementation and guaranteed O(n*log(n)) complexity.

PHP手册中的示例代码-

A sample code from the PHP Manual -

function cmp( $a, $b ) { 
  if(  $a->weight ==  $b->weight ){ return 0 ; } 
  return ($a->weight < $b->weight) ? -1 : 1;
} 

usort($unsortedObjectArray,'cmp');

这篇关于PHP中的冒泡排序实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆