Ordenando arrays - Bubble Sort PHP

Thursday, 28 August 2008 16:07 por leolima77
Agora em PHP, ordenando array com o método Bubble Sort

<?php
function BubbleSort( $items ) {
$temp = "";
$size = count( $items );
for( $i = 1; $i < $size; $i++ ) {
for( $j = 1; $j < $size - $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 );
?>

Currently rated 4.0 by 2 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categorias:   Dicas
Ferramentas:   E-mail | del.icio.us | digg | rec6 | linkk | Permalink