Array sort functions in PHP - Time & Space Complexity
Sorting arrays is a common task in programming. Knowing how the time needed grows as the array gets bigger helps us write better code.
We want to understand how sorting time changes when the array size increases.
Analyze the time complexity of the following code snippet.
$array = [5, 3, 8, 1, 2];
sort($array);
This code sorts an array of numbers in ascending order using PHP's built-in sort function.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing and swapping elements during sorting.
- How many times: Depends on the sorting algorithm, but generally multiple passes over the array elements.
As the array gets bigger, the number of comparisons and swaps grows faster than the size itself.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 35 operations |
| 100 | About 700 operations |
| 1000 | About 10,000 operations |
Pattern observation: The operations grow roughly as n log n.
Time Complexity: O(n log n)
This means the time to sort grows a bit faster than the size of the array but much slower than if it grew with the square of the size.
[X] Wrong: "Sorting always takes the same time no matter the array size."
[OK] Correct: Sorting time depends on how many items there are; bigger arrays take more time because more comparisons and swaps are needed.
Understanding sorting time helps you explain your choices and write efficient code, a skill valued in many programming tasks.
"What if we used a simple bubble sort instead of PHP's built-in sort? How would the time complexity change?"
Practice
Solution
Step 1: Understand key preservation in sorting
asort()sorts values but keeps the original keys unchanged.Step 2: Compare with other functions
sort()resets keys,rsort()sorts in reverse but resets keys,ksort()sorts by keys, not values.Final Answer:
asort() -> Option DQuick Check:
Keep keys while sorting values = asort() [OK]
- Confusing sort() with asort() for key preservation
- Thinking ksort() sorts by values
- Assuming rsort() keeps keys
Solution
Step 1: Identify function for ascending sort with key reset
sort()sorts array values in ascending order and resets keys.Step 2: Check other options
asort()keeps keys,ksort()sorts by keys,arsort()sorts descending.Final Answer:
sort($array); -> Option BQuick Check:
Ascending sort with reset keys = sort() [OK]
- Using asort() when keys should reset
- Confusing ksort() with value sort
- Using arsort() for ascending order
$arr = [3 => 'apple', 1 => 'banana', 2 => 'cherry']; sort($arr); print_r($arr);
Solution
Step 1: Understand sort() behavior on keys
sort()sorts values ascending and resets keys starting from 0.Step 2: Sort values and assign new keys
Values sorted ascending: 'apple', 'banana', 'cherry'. Keys reset to 0,1,2 respectively.Final Answer:
[0 => 'apple', 1 => 'banana', 2 => 'cherry'] -> Option AQuick Check:
sort() resets keys and sorts ascending [OK]
- Expecting original keys to remain
- Confusing sort() with asort()
- Mixing order of sorted values
$arr = ['b' => 2, 'a' => 1, 'c' => 3]; ksort($arr, SORT_DESC); print_r($arr);
Solution
Step 1: Check ksort() parameters
ksort()sorts array by keys in ascending order only; it does not support descending order.Step 2: Understand SORT_DESC usage
SORT_DESCis invalid for ksort(); to sort keys descending, use krsort() instead.Final Answer:
ksort() does not accept SORT_DESC as a parameter -> Option CQuick Check:
ksort() only sorts keys ascending [OK]
- Passing SORT_DESC to ksort()
- Thinking ksort() sorts values
- Believing print_r() can't print associative arrays
$scores = [101 => 50, 102 => 75, 103 => 50, 104 => 90];You want to sort the array by scores in descending order but keep the user IDs as keys. Which function should you use?
Solution
Step 1: Identify sorting by values descending with keys preserved
arsort()sorts array by values in descending order and keeps keys intact.Step 2: Compare with other functions
rsort()sorts values descending but resets keys,krsort()sorts keys descending,sort()sorts ascending and resets keys.Final Answer:
arsort() -> Option AQuick Check:
Sort values descending and keep keys = arsort() [OK]
- Using rsort() which resets keys
- Confusing krsort() with value sort
- Using sort() which sorts ascending and resets keys
Ready to prove it?
Pass the test and this pattern moves unknown
