|
 |
13³â Àü |
PHP ¹öºí¼ÒÆ® ¾Ë°í¸®Áò
-------------------------------------------------------------------------------------------
<?php
/*
Esta função troca o valor de duas variáveis entre si.
Opcional. Pode ser embutido na bolha
*/
function swap(&$valor_1, &$valor_2) {
$valor_1_antigo = $valor_1;
$valor_2_antigo = $valor_2;
$valor_1 = $valor_2_antigo;
$valor_2 = $valor_1_antigo;
}
/* Array de teste */
$arrToSort = array(1, 4, 7, 3, 8, 9, 10);
/* a BOLHA! ;-) */
for ($i = 0; $i < count($arrToSort); $i++) {
for ($j = $i; $j < count($arrToSort); $j++) {
if ($arrToSort[$i] > $arrToSort[$j]) {
swap($arrToSort[$i], $arrToSort[$j]);
}
}
}
/* Opcional. Exibe o array de um jeito que nós podemos entender! =D */
print_r($arrToSort);
?>
-------------------------------------------------------------------------------------------
<?php
function BubbleSort( $items ) {
$temp = "";
$size = count( $items );
for( $i = 1; $i <= $size-1; $i++ ) {
for( $j = 1; $j <= $size - 1 - $i; $j++ ) {
if( $items[$j+1] < $items[$j] ) {
$temp = $items[$j];
$items[$j] = $items[$j+1];
$items[$j+1] = $temp;
}
}
}
}
$items = array(31, 41, 59, 26, 41, 58);
BubbleSort( $items );
?>
-------------------------------------------------------------------------------------------
|
|
̵̧ : 525 |
̵̧
¸ñ·Ï
|
|