Sorry, the title is misleading
, I did not find some algorithm faster than quicksort, in fact, quicksort's lower bound is proven the lowest for any comparison sort algorithm.
Two days ago, a article about using quicksort algorithm in PHP by Tim Koschuetzki caught my eyes.
Koschuetzki was working on an array somewhat like this:
Array
(
[0] => Array
(
[idv11] => 624059d1f8N0
[adspots] => 2
)
[1] => Array
(
[idv11] => 67059d1f8N0
[adspots] => 4
)
[2] => Array
(
[idv11] => 6759d1f8N0
[adspots] => 8
)
)Usually, problems like this are taking care by the database, I don't know why Koschuetzki's corporation ask the sorting to be done in PHP. So I come up with something extremely fast.
Below is the simplified version of Koschuetzki's quicksort. I created it by modifying the PHP quicksort implementation posted on wikibook.
function quicksort_by_adspots($array){ if(!count($array)) return $array; $k = $array[0]['adspots']; $own = $array[0]; $x = $y = array(); $count = count($array); for($i=1; $i<$count; $i++) { if($array[$i]['adspots'] <= $k) { $x[] = $array[$i]; } else { $y[] = $array[$i]; } } return array_merge(quicksort_by_adspots($x), array($own), quicksort_by_adspots($y)); }
function usort_by_adspots($array){ function adcomp($a, $b){ if ($a['adspots'] == $b['adspots']) { return 0; } return ($a['adspots'] < $b['adspots']) ? -1 : 1; } usort($array, "adcomp"); return $array; }
I guess this shows the importance of read the manual, I check out the entire manual and read each one of them today and finally found what I need. usort is the last array function in the manual.
PHP implementation of Pancake sort, the very mathy but slow(at least in PHP) sort.
function pancake_sort($array){ $count = count($array); $powercount = $count - 1; $i = 0; while($i < $powercount){ $n = 0; $largest = 0; while($n < $count){ if($array[$largest]<$array[$n]){ $largest = $n; } ++$n; } $num = $largest/2; $j = 0; while($j < $num){ $z = $array[$largest-$j]; $array[$largest-$j] = $array[$j]; $array[$j] = $z; ++$j; } $num = --$count/2; $j = 0; while($j<$num){ $z = $array[$count-$j]; $array[$count-$j] = $array[$j]; $array[$j] = $z; ++$j; } ++$i; } return $array; }
Bozo Sort is more efficient than than bogosort(in theory). It does sorting by randomly chose 2 items and compare them, then switch them if needed. Until everything is sorted.
function bozo_sort($array){ $sorted = $array; sort($sorted); while($sorted !== $array){ $i = array_rand($array); $j = array_rand($array); if($i < $j){ if($array[$i] > $array[$j]){ $z = $array[$j]; $array[$j] = $array[$i]; $array[$i] = $z; } }else{ if($array[$j] > $array[$i]){ $z = $array[$i]; $array[$i] = $array[$j]; $array[$j] = $z; } } } return $array; }
PHP implementation of Dropsort, a lossy sort algorithm that will drop anything that is smaller than the previous item.
function dropsort($array){ $i = 0; while($array[$i]){ if($array[$i] <= $array[$i+1]||!isset($array[$i+1])){ ++$i; }else{ array_splice($array, $i+1, 1); } } return $array; }
Editor Comment:
After making the Intelligent Design Sort work in PHP, this is another esoteric algorithm I implant in PHP. I guess I will make all the esoteric algorithms featured in DM's Esoteric Programming Languages work in PHP
Recent comments
3 hours 40 min ago
3 days 52 min ago
3 days 9 hours ago
1 week 21 hours ago
1 week 2 days ago
1 week 2 days ago
1 week 4 days ago
2 weeks 1 day ago
2 weeks 1 day ago
2 weeks 4 days ago