Php 泡泡糖做了些奇怪的事
我正在慢慢地试图弄明白冒泡排序的实现,这个概念很容易理解。基本上我已经做到了这一点:Php 泡泡糖做了些奇怪的事,php,bubble-sort,Php,Bubble Sort,我正在慢慢地试图弄明白冒泡排序的实现,这个概念很容易理解。基本上我已经做到了这一点: <?php namespace TeamRock; class BubbleSort { public function sort(array $integers) { if (count ($integers) > 0) { //if value of array is over one do this--> for ($i = 0; $
<?php
namespace TeamRock;
class BubbleSort
{
public function sort(array $integers)
{
if (count ($integers) > 0)
{
//if value of array is over one do this-->
for ($i = 0; $i<count($integers); $i++) //iterating through the array
{
for($j = 1; $j<count($integers);$j++)
{
//where the sorting happens
$holder = $integers[$j];
if ($integers[$j] < $integers[$j-1]){
$integers[$i] = $integers[$j-1];
$integers[$j-1] = $holder;
}
}
}
return $integers;
}
else{
return $integers;
}
}
}
Sudo Code-
//For each element in array
//Look at the element to direct right
//If the element on the left is greater than the element to the direct right
//Then we should swap these elements positions
//
//restart at start of loop until all numbers are numbered
任何帮助或指示都将不胜感激
我确实有一个插入排序工作良好,这就是为什么有一些通过,失败的是泡沫
再一次,我对此很陌生,所以给我一点喘息的空间,让我不要错过任何基本的东西。我试图在脑子里记住这一点:首先,使用冒泡排序不是一个好主意。它的复杂性为O(n^2)。您应该使用phpusort,它实际上是一个合并排序实现,具有(n*log(n))复杂性,或者您可以自己实现它 无论如何,您的冒泡排序是错误的,您混淆了索引 试试这个:
public function bubbleSort(array $numbers)
{
for ( $i = 0; $i < count($numbers); $i++ ) {
for ($j = 0; $j < count($numbers) $j++ ) {
if ($numbers[$i] < $numbers[$j]) {
$temp = $numbers[$i];
$numbers[$i] = $numbers[$j];
$numbers[$j] = $temp;
}
}
}
return $numbers;
}
公共函数bubbleSort(数组$numbers)
{
对于($i=0;$i
您的代码中有一些错误。首先,算法:
$holder = $integers[$j];
if ($integers[$j] < $integers[$j-1]){
$integers[$i] = $integers[$j-1];
$integers[$j-1] = $holder;
}
因为您正在用$integers[$j]
交换$integers[$j-1]
,如果它们的顺序不正确(我确信这只是一个输入错误)。此错误可能导致规范中的第二个测试用例失败 第一个测试用例:
function it_can_sort_an_array_of_four_integers()
{
$integers = [8, 4, 6, 2];
$this->sort($integers)->shouldReturn(['2, 4, 6, 8']);
}
应改为:
$integers[$j] = $integers[$j-1];
$this->sort($integers)->shouldReturn([2, 4, 6, 8]);
请注意,这是一个由四个整数组成的数组,而代码将根据包含一个字符串的数组检查结果
进一步改进:
运行count($integers)
将通过数组检查顺序错误的相邻值对。虽然这是所需的最大通过次数,但很多时候它会提前完成
一个更好的实现是保留一个标记,该标记在每次传递后都会记住是否有和交换完成,并在没有时退出循环(因为数组已经排序)
大概是这样的:
public function sort(array $integers)
{
// Call count() only once before the loop because it doesn't change
$count = count($integers);
do {
// Didn't swap any neighbour values yet in this loop
$swapped = FALSE;
// Run through the list, swap neighbour values if needed
for ($j = 1; $j < $count; $j ++)
{
// Check neighbour values
// Initialize $holder only when it is needed (to swap values)
if ($integers[$j] < $integers[$j - 1]) {
// Swap the values
$holder = $integers[$j];
$integers[$i] = $integers[$j - 1];
$integers[$j - 1] = $holder;
// Remember we did at least one swap on this pass
$swapped = TRUE;
}
}
// Keep passing through the array while at least one swap was done
// When there was no swap then the values are in the desired order
} while ($swapped);
// Return the sorted array
return $integers;
}
公共函数排序(数组$整数)
{
//在循环之前只调用count(),因为它不会更改
$count=计数($integers);
做{
//在此循环中尚未交换任何相邻值
$swapped=FALSE;
//浏览列表,必要时交换相邻值
对于($j=1;$j<$count;$j++)
{
//检查相邻值
//仅在需要时初始化$holder(交换值)
if($integers[$j]<$integers[$j-1]){
//交换值
$holder=$integers[$j];
$integers[$i]=$integers[$j-1];
$integers[$j-1]=$holder;
//还记得我们在这个关卡上至少交换了一次吗
$swapped=TRUE;
}
}
//在至少完成一次交换时,继续通过阵列
//如果没有交换,则值按所需顺序排列
}而(以美元交换),;
//返回排序后的数组
返回$整数;
}
好吧,等我想明白了之后,我再看一下,我想了解一下。谢谢你的帮助!这不是冒泡排序,它的性能甚至比OP的算法还要差。感谢您深入讲解我的错误,非常有帮助,非常感谢!!是的,我弄错了,再次谢谢你,先生,帮了大忙!
$this->sort($integers)->shouldReturn([2, 4, 6, 8]);
public function sort(array $integers)
{
// Call count() only once before the loop because it doesn't change
$count = count($integers);
do {
// Didn't swap any neighbour values yet in this loop
$swapped = FALSE;
// Run through the list, swap neighbour values if needed
for ($j = 1; $j < $count; $j ++)
{
// Check neighbour values
// Initialize $holder only when it is needed (to swap values)
if ($integers[$j] < $integers[$j - 1]) {
// Swap the values
$holder = $integers[$j];
$integers[$i] = $integers[$j - 1];
$integers[$j - 1] = $holder;
// Remember we did at least one swap on this pass
$swapped = TRUE;
}
}
// Keep passing through the array while at least one swap was done
// When there was no swap then the values are in the desired order
} while ($swapped);
// Return the sorted array
return $integers;
}