HiveBrain v1.2.0
Get Started
← Back to all entries
snippetphpMinor

Quick sort in PHP

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sortphpquick

Problem

Does this code show proper implementation of quick sort algorithm? If not, why? Can this code further be improved?

';
print_r($unsorted);
print_r(quick_sort($unsorted));

?>

Solution

The else statement for array length check is superfluous. Also, your pivot choosing policy has a flaw: on already sorted array it will degrade to quadratic running time; I suggest you choose the pivot randomly or choose the pivot from several elements.

You don't need to do that array creation stuff you seem to do: quicksort can be implemented in-place.

Context

StackExchange Code Review Q#82834, answer score: 2

Revisions (0)

No revisions yet.