Cheapest mathmatical mutli-dimensonal array sort cost?
Posted: Thu Nov 06, 2008 12:50 pm
Hi guys, I ran into a problem.
So currently I have an array like this:
etc.
My goal is to sort this array by age.
Right now I am currently sorting it with array_multisort() which we know is an O(nlogn) algorithm (since all PHP sorting methods are implementations of quicksort).
However before you can use this function I need to run a foreach loop on $users to make an index for the array_multisort() function:
Running this snip of code gives us a running time O(n).
My question is this. Does this slow down the overall performance? I am shooting for an algorithm that MUST BE an O(nlogn) running time (the worst case O(n^2) from quicksort is ok too) but the current way I have it set up it performs O(nlogn) + O(n).
Any thoughts or how to better approach this?
Thanks for any help
So currently I have an array like this:
Code: Select all
$users = array(array('id'=> 1, 'username' => 'user', 'age' => 42),
array('id'=> 2, 'username' => 'user', 'age' => 22));etc.
My goal is to sort this array by age.
Right now I am currently sorting it with array_multisort() which we know is an O(nlogn) algorithm (since all PHP sorting methods are implementations of quicksort).
However before you can use this function I need to run a foreach loop on $users to make an index for the array_multisort() function:
Code: Select all
foreach $users as $key => $row) {
$user_age[$key] = $row['age'];
}
My question is this. Does this slow down the overall performance? I am shooting for an algorithm that MUST BE an O(nlogn) running time (the worst case O(n^2) from quicksort is ok too) but the current way I have it set up it performs O(nlogn) + O(n).
Any thoughts or how to better approach this?
Thanks for any help