Snippets v2

Languages
My Snippets

Bubblesort Login or Register to Bookmark this snippet

Description PHP

This is a simple bubblesort by Petter Nilsenthat takes 2 arrays as argument.The first one is the actual data used for sorting, the second is data that will "tag along" with the first array, for instance a descriptive text about the data in the first array.

The Code Download
  1. <?php
  2. function BubbleSort2($sort_array,$column = 0,$reverse) {
  3.     $lunghezza=count($sort_array);
  4.     for ($i = 0; $i < $lunghezza ; $i++){
  5.         for ($j = $i + 1; $j < $lunghezza ; $j++){
  6.             if($reverse){
  7.                 if ($sort_array[$i][$column] < $sort_array[$j][$column]){
  8.                     $tmp = $sort_array[$i];
  9.                     $sort_array[$i] = $sort_array[$j];
  10.                     $sort_array[$j] = $tmp;
  11.                 }
  12.             }else{
  13.                 if ($sort_array[$i][$column] > $sort_array[$j][$column]){
  14.                     $tmp = $sort_array[$i];
  15.                     $sort_array[$i] = $sort_array[$j];
  16.                     $sort_array[$j] = $tmp;
  17.                 }
  18.             }
  19.         }
  20.     }
  21.     return $sort_array;          
  22. }
  23. ?>
Credits Contact JC

Added by JC on 14th November, 2007

Comments

There are no comments about this snippet.

Post Comment HTML is allowed

You must be registered and logged in to post a comment.

Login here to post a comment