算法系列–冒泡排序

phpman.song@gmail.com发布

定义

冒泡排序是用来排列一连串数字的算法

原理

  1. 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
  2. 对每一对相邻元素做同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。
  3. 针对所有的元素重复以上的步骤,除了最后一个。
  4. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。

PHP实现

PHP沙盒环境

拷贝代码到沙盒环境可以看到效果

//PHP实现升序
function bubble_asort(array $numbers = []){
  //获取数组元素个素总数 $n
  $n = count($numbers);
  //控制循环次数 $n-1
  for($j= 1; $j< $n; $j++){
   //控制元素坐标 $n-$j
    for($k = 0; $k < $n-$j; $k++){
      if($numbers[$k] > $numbers[$k + 1]){
        $temp = $numbers[$k];
        $numbers[$k] = $numbers[$k+1];
        //元素值大的往后排
        $numbers[$k+1] = $temp;
      }
    }
  }
  return $numbers;
}

//PHP实现降序
function bubble_dsort(array $numbers = []){
  //获取数组元素个素总数 $n
  $n = count($numbers);
  //控制循环次数 $n-1
  for($j= 1; $j< $n; $j++){
    //控制元素坐标 $n-$j
    for($k = 0; $k < $n-$j; $k++){
      if($numbers[$k] < $numbers[$k + 1]){
        $temp = $numbers[$k];
        $numbers[$k] = $numbers[$k+1];
        //元素值小的往后排
        $numbers[$k+1] = $temp;
      }

    }
  }
  return $numbers;
}

//例子
$numbers = [1,3,4,22,88,33,31,42];
print_r(bubble_asort($numbers));
print_r(bubble_dsort($numbers));

//PHP内置函数实现
sort($numbers,1);
print_r($numbers);

rsort($numbers,1);
print_r($numbers);

分类: 算法

14 条评论

fiat 500 replacement key · 2021年1月5日 上午7:45

Perfect piece of work you have done, this internet site is really cool with fantastic information.

renault clio key replacement · 2021年1月6日 上午1:04

I’d always want to be update on new blog posts on this site, saved to bookmarks!

sildenafil prices · 2021年1月7日 上午11:16

I happen to be writing to make you understand what a terrific experience our princess undergone visiting yuor web blog. She picked up such a lot of things, with the inclusion of what it is like to possess an incredible teaching style to make the others without hassle thoroughly grasp specified tricky topics. You undoubtedly surpassed visitors’ expected results. Thanks for showing those valuable, trustworthy, revealing not to mention unique tips on your topic to Sandra.

viagra online · 2021年1月8日 上午2:14

Thanks for your own labor on this web site. My mother take interest in participating in research and it is simple to grasp why. My spouse and i notice all regarding the powerful way you produce useful thoughts via your website and in addition recommend response from the others on this subject then our own princess is undoubtedly studying a whole lot. Take pleasure in the rest of the year. You have been performing a terrific job.

cialis without a prescription · 2021年1月8日 上午5:09

I’m commenting to let you be aware of of the awesome discovery my child developed browsing the blog. She came to find a good number of details, including how it is like to have a great coaching character to get a number of people clearly completely grasp a number of advanced issues. You actually did more than her desires. I appreciate you for churning out such effective, dependable, edifying and cool guidance on that topic to Emily.

发表评论

邮箱地址不会被公开。 必填项已用*标注