View Single Post
luke Male
KittyLover
Administrator
 
luke's Avatar
 
Join Date: Nov 2003
Posts: 1,878
  #1 Old 01-12-2008 Smile Bubble Sort in various programming languages

Just for fun, let's share codes for Bubble Sort algorithm in any programming languages here.

PHP
Code:
<?php

$data = array(
'Perlis', 'Kedah', 'Pulau Pinang',
'Perak', 'Selangor', 'Wilayah Persekutuan',
'Negeri Sembilan', 'Melaka', 'Johor',
'Pahang', 'Terengganu', 'Kelantan',
'Sarawak', 'Sabah');

for ($i = 0; $i < count($data); $i++) {
  for ($j = count($data) - 1; $j > $i; $j--) {
    if ($data[$j] < $data[$j - 1]) {
      $t = $data[$j];
      $data[$j] = $data[$j - 1];
      $data[$j - 1] = $t;
    }
  }
}

print '<pre>' . print_r($data, true) . '</pre>';
?>
__________________
luke is offline   Reply With Quote
 
Page generated in 0.05550 seconds with 13 queries