当前位置:   article > 正文

49、PHP 实现堆排序

49、PHP 实现堆排序

题目: PHP 实现堆排序

描述:

  • 堆排序
  • 基本思想:
  • 堆排序(HeapSort)是一树形选择排序。
  • 在排序过程中,将R[l…n]看成是一棵完全二叉树的顺序存储结构,
  • 利用完全二叉树中双亲结点和孩子结点之间的内在关系,
  • 在当前无序区中选择关键字最大(或最小)的记录。
  • 根结点(亦称为堆顶)的关键字是堆里所有结点关键字中最小者的堆称为小根堆。
  • 根结点(亦称为堆顶)的关键字是堆里所有结点关键字中最大者,称为大根堆。
function heapSort(array $list){
	$length = count($list);
	buildHeap($list, $length - 1);
	while(--$length){
		$temp = $list[0];
		$list[0] = $list[$length];
		$list[$length] = $temp;
		heapAdjust($list, 0, $length);
	}
	return $list;
}

function buildHeap(array &$list, $num){
	for($i = floor(($num - 1) / 2); $i >= 0; $i--){
		heapAdjust($list, $i, $num + 1);
	}
}

function heapAdjust(array &$list, $i, $num){
	if($i > $num / 2){
		return ;
	}

	$key = $i;
	$leftChild = $i * 2 + 1;
	$rightChild = $i * 2 + 2;
	if ($leftChild < $num && $list[$leftChild] > $list[$key]) {
	    $key = $leftChild;
	}
	if ($rightChild < $num && $list[$rightChild] > $list[$key]) {
	    $key = $rightChild;
	}
	if ($key != $i) {
	    $val = $list[$i];
	    $list[$i] = $list[$key];
	    $list[$key] = $val;
	    heapAdjust($list, $key, $num);
	    // heapPrint($list);
	}
}

// function heapPrint(array $list){
// 	echo '<br>';
// 	echo 'Memory Structure:';
// 	echo '[ ' . implode(', ', $list) . ' ]';
// 	echo '<br>';
// 	echo 'Heap:';
// 	echo '<br>';
// 	$nbsp = '&nbsp;';
// 	$length = count($list);
// 	$level = ceil(sqrt($length));
// 	//start index
// 	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 
 * 8  10 1  9 
 * 4  5  7 

 * Memory Structure:[ 3, 6, 9, 8, 10, 1, 2, 4, 5, 7 ]
 * Heap:
 * 3 
 * 6  9 
 * 8  10 1  2 
 * 4  5  7 

 * Memory Structure:[ 3, 10, 9, 8, 7, 1, 2, 4, 5, 6 ]
 * Heap:
 *  3 
 *  10  9 
 *  8  7  1  2 
 *  4  5  6 

 * Memory Structure:[ 3, 10, 9, 8, 7, 1, 2, 4, 5, 6 ]
 * Heap:
 *  3 
 *  10  9 
 *  8  7  1  2 
 *  4  5  6 

 * Memory Structure:[ 10, 8, 9, 5, 7, 1, 2, 4, 3, 6 ]
 * Heap:
 *  10 
 *  8  9 
 *  5  7  1  2 
 *  4  3  6 

 * Memory Structure:[ 10, 8, 9, 5, 7, 1, 2, 4, 3, 6 ]
 * Heap:
 *  10 
 *  8  9 
 *  5  7  1  2 
 *  4  3  6 

 * Memory Structure:[ 10, 8, 9, 5, 7, 1, 2, 4, 3, 6 ]
 * Heap:
 *  10 
 *  8  9 
 *  5  7  1  2 
 *  4  3  6 

 * Memory Structure:[ 9, 8, 6, 5, 7, 1, 2, 4, 3, 10 ]
 * Heap:
 *  9 
 *  8  6 
 *  5  7  1  2 
 *  4  3  10 

 * Memory Structure:[ 8, 7, 6, 5, 3, 1, 2, 4, 9, 10 ]
 * Heap:
 *  8 
 *  7  6 
 *  5  3  1  2 
 *  4  9  10 

 * Memory Structure:[ 8, 7, 6, 5, 3, 1, 2, 4, 9, 10 ]
 * Heap:
 *  8 
 *  7  6 
 *  5  3  1  2 
 *  4  9  10 

 * Memory Structure:[ 7, 5, 6, 4, 3, 1, 2, 8, 9, 10 ]
 * Heap:
 *  7 
 *  5  6 
 *  4  3  1  2 
 *  8  9  10 

 * Memory Structure:[ 7, 5, 6, 4, 3, 1, 2, 8, 9, 10 ]
 * Heap:
 *  7 
 *  5  6 
 *  4  3  1  2 
 *  8  9  10 

 * Memory Structure:[ 6, 5, 2, 4, 3, 1, 7, 8, 9, 10 ]
 * Heap:
 *  6 
 *  5  2 
 *  4  3  1  7 
 *  8  9  10 

 * Memory Structure:[ 5, 4, 2, 1, 3, 6, 7, 8, 9, 10 ]
 * Heap:
 *  5 
 *  4  2 
 *  1  3  6  7 
 *  8  9  10 

 * Memory Structure:[ 5, 4, 2, 1, 3, 6, 7, 8, 9, 10 ]
 * Heap:
 *  5 
 *  4  2 
 *  1  3  6  7 
 *  8  9  10 

 * Memory Structure:[ 4, 3, 2, 1, 5, 6, 7, 8, 9, 10 ]
 * Heap:
 *  4 
 *  3  2 
 *  1  5  6  7 
 *  8  9  10 

 * Memory Structure:[ 3, 1, 2, 4, 5, 6, 7, 8, 9, 10 ]
 * Heap:
 *  3 
 *  1  2 
 *  4  5  6  7 
 *  8  9  10 

 * Memory Structure:[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
 * Heap:
 *  1 
 *  2  3 
 *  4  5  6  7 
 *  8  9  10 
 */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/919763
推荐阅读
相关标签
  

闽ICP备14008679号