} } } #交换父节点和最大节点 if ($start != $maxInx) { $temp = $arr[$start]; $arr[$start] = $arr[$maxInx]; $arr[$maxInx] = $temp; #如果交换后的子节点还有子节点,继续调整 if (($maxInx + 1) * 2 <= $len) { ajustNodes($arr, $maxInx, $end); } } } $arr = array($text0, $text1, $text2, $text3, $text4, $text5, $text6, $text7, $text8); heapSort($arr); if ($text0 == null || $text1 == null) { echo "<script>" . "alert('你没有提交数据,请确定输入数据后再提交')" . "</script>"; } else { print_r($arr); echo "</p>" . "<table width='650' border='1' bgcolor='#FFFFFF' cellspacing='0'>" . "<font size='2'>" . "<th>" . "原理:利用堆积树排序算法,利用数组的特点快速定位指定索引的元素,分大根堆和小根堆,最大值一定在堆顶。" . "</font>" . "</table>"; } } else { if ($choose == "二路归并") { //merge函数将指定的两个有序数组(arr1arr2,)合并并且排序 //我们可以找到第三个数组,然后依次从两个数组的开始取数据哪个数据小就先取哪个的,然后删除掉刚刚取过///的数据 function al_merge($arrA, $arrB) { $arrC = array(); while (count($arrA) && count($arrB)) { //这里不断的判断哪个值小,就将小的值给到arrC,但是到最后肯定要剩下几个值,
// for($j = 0; $j < $level; $j++){ // $startIndex = pow(2, $j) - 1; // $endIndex = pow(2, $j) + $startIndex; // for($i = $startIndex; $i < $endIndex; $i++){ // if($i < $length){ // echo $nbsp; // echo $list[$i]; // echo $nbsp; // } // } // echo '<br>'; // } // } $list = array(3, 6, 2, 4, 10, 1, 9, 8, 5, 7); // heapPrint($list); $result = heapSort($list); // heapPrint($result); var_dump($result); /** * 分析: * Memory Structure:[ 3, 6, 2, 4, 10, 1, 9, 8, 5, 7 ] * Heap: * 3 * 6 2 * 4 10 1 9 * 8 5 7 * * Memory Structure:[ 3, 6, 2, 8, 10, 1, 9, 4, 5, 7 ] * Heap: * 3 * 6 2