Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中进行堆排序?_Php_Sorting_Heap - Fatal编程技术网

如何在php中进行堆排序?

如何在php中进行堆排序?,php,sorting,heap,Php,Sorting,Heap,我正在用php编写关于堆排序的赋值。我的处境有点困难。请帮帮我。任何向我推荐代码的人。 提前谢谢。你用谷歌搜索过这个吗??? 看看这个。 欢迎来到SO!如果你能说明你已经做了多少,以及你到底遇到了什么问题,你就更有可能得到有用的回答。如果php是个问题,你可以从编写伪代码开始,然后得到翻译它的帮助。我如何在php中对名称数组进行堆排序(字母排序)?不是整数。 <?php function build_heap(&$array, $i, $t){ $tmp_var = $a

我正在用php编写关于堆排序的赋值。我的处境有点困难。请帮帮我。任何向我推荐代码的人。 提前谢谢。

你用谷歌搜索过这个吗??? 看看这个。



欢迎来到SO!如果你能说明你已经做了多少,以及你到底遇到了什么问题,你就更有可能得到有用的回答。如果php是个问题,你可以从编写伪代码开始,然后得到翻译它的帮助。我如何在php中对名称数组进行堆排序(字母排序)?不是整数。
<?php
 function build_heap(&$array, $i, $t){
  $tmp_var = $array[$i];    
  $j = $i * 2 + 1;

  while ($j <= $t)  {
   if($j < $t)
    if($array[$j] < $array[$j + 1]) {
     $j = $j + 1; 
    }
   if($tmp_var < $array[$j]) {
    $array[$i] = $array[$j];
    $i = $j;
    $j = 2 * $i + 1;
   } else {
    $j = $t + 1;
   }
  }
  $array[$i] = $tmp_var;
 }

 function heap_sort(&$array) {
  //This will heapify the array
  $init = (int)floor((count($array) - 1) / 2);
  // Thanks jimHuang for bug report
  for($i=$init; $i >= 0; $i--){
   $count = count($array) - 1;
   build_heap($array, $i, $count);
  }

  //swaping of nodes
  for ($i = (count($array) - 1); $i >= 1; $i--)  {
   $tmp_var = $array[0];
   $array [0] = $array [$i];
   $array [$i] = $tmp_var;
   build_heap($array, 0, $i - 1);
  }
 }

// Demo
$array = array(9,8,7,6,5,4,3,2,1,0,10,1000,0);
heap_sort($array);
print_r($array);
?>