Changeset 1585

Show
Ignore:
Timestamp:
12/17/2007 10:26:14 PM (11 months ago)
Author:
zombor
Message:

removing the hand coded search functions...built in sort() is 400x faster than a hand build merge sort.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/helpers/arr.php

    r1584 r1585  
    8484         * @param  array    an array of values to search in 
    8585         * @param  boolean  return false, or the nearest value 
    86          * @param  mixed    sort the array by the requested sorting method (available types are bubble, insertion, quick, selection, merge) 
     86         * @param  mixed    sort the array before searching it 
    8787         * @return integer 
    8888         */ 
    8989        public function binary_search($needle, $haystack, $nearest = FALSE, $sort = FALSE) 
    9090        { 
    91                 if ($sort != FALSE AND is_string($sort)) 
     91                if ($sort) 
    9292                { 
    93                         $method = $sort.'_sort'; 
    94                         self::$method($haystack); 
     93                        sort($haystack); 
    9594                } 
    9695 
     
    125124                        return $high; 
    126125        } 
    127  
    128         /** 
    129          * Bubble sort algorithm. 
    130          * 
    131          * @param  array the array to sort 
    132          */ 
    133         public function bubble_sort(&$array) 
    134         { 
    135                 $size = count($array); 
    136                 do 
    137                 { 
    138                         $swapped = FALSE; 
    139                         $size--; 
    140                         for ($i = 0; $i < $size; $i++) 
    141                         { 
    142                                 if ($array[$i] > $array[$i+1]) 
    143                                 { 
    144                                         // Swap the values 
    145                                         $temp = $array[$i+1]; 
    146                                         $array[$i+1] = $array[$i]; 
    147                                         $array[$i] = $temp; 
    148                                         $swapped = TRUE; 
    149                                 } 
    150                         } 
    151                 } while ($swapped); 
    152         } 
    153  
    154         /** 
    155          * Insertion sort algorithm. 
    156          * 
    157          * @param  array the array to sort 
    158          */ 
    159         public function insertion_sort(&$array) 
    160         { 
    161                 for ($i = 0; $i < count($array); $i++) 
    162                 { 
    163                         $value = $array[$i]; 
    164                         $j = $i - 1; 
    165                         while ($j >= 0 AND $array[$j] > $value) 
    166                         { 
    167                                 $array[$j+1] = $array[$j]; 
    168                                 $j--; 
    169                         } 
    170                         $array[$j+1] = $value; 
    171                 } 
    172         } 
    173  
    174         /** 
    175          * Quick sort algorithm. 
    176          * 
    177          * @param  array the array to sort 
    178          * @param  integer low position to start at 
    179          * @param  integer high position to start at 
    180          * @param  integer minumum number of items to sort. If below the value, we go to insertion sort since its faster on small datasets 
    181          */ 
    182         public function quick_sort(&$array, $low_pos, $high_pos, $cutoff = 1500) 
    183         { 
    184                 // Quick sort is actually slow on small data sets 
    185                 if($low_pos + $cutoff > $high_pos) 
    186                 {  
    187                         self::insertion_sort($array);  
    188                 } 
    189                 else 
    190                 { 
    191                         $left = $low_pos; 
    192                         $right = $high_pos; 
    193                         $pivot = $array[$left]; 
    194                         while ($left < $right) 
    195                         {  
    196                                 while (($array[$right] >= $pivot) && ($left < $right)) 
    197                                 { 
    198                                         $right--; 
    199                                 }  
    200                                 if ($left != $right) 
    201                                 { 
    202                                         $array[$left] = $array[$right]; 
    203                                         $left++; 
    204                                 } 
    205                                 while (($array[$left] <= $pivot) && ($left < $right))  
    206                                 { 
    207                                         $left++;  
    208                                         if ($left != $right) 
    209                                         {  
    210                                                 $array[$right] = $array[$left];  
    211                                                 $right--;  
    212                                         }  
    213                                 }  
    214                                 $array[$left] = $pivot;  
    215                                 $pivot = $left;  
    216                                 if ($low_pos < $pivot) 
    217                                 { 
    218                                         self::qsort($array, $low_pos, $pivot, $cutoff); 
    219                                 } 
    220                                 if ($high_pos > $pivot) 
    221                                 { 
    222                                         self::qsort($array, $pivot+1, $high_pos, $cutoff);  
    223                         } 
    224                         } 
    225                 } 
    226         } 
    227  
    228         /** 
    229          * Selection sort algorithm. 
    230          * 
    231          * @param  array the array to sort 
    232          */ 
    233         public function selection_sort(&$array) 
    234         { 
    235                  
    236         } 
    237  
    238         /** 
    239          * Merge sort algorithm. 
    240          * 
    241          * @param  array the array to sort 
    242          */ 
    243         public function merge_sort(&$array) 
    244         { 
    245                  
    246         } 
    247126} // End arr