Advertisement
Guest User

exp

a guest
Sep 26th, 2017
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. 1) How does the comparator work?
  2.  
  3. I'm not sure wether this was part of the question, but to be clear about how the comparator works: You have an order specified by ordered list $order and a special comparator callback list_cmp which (should) return wether argument
  4. - $a is smaller than $b (return -1 or a value < 0)
  5. - $a greater than $b (return 1 or a value > 0)
  6. - $a equal $b (return 0)
  7.  
  8. list_cmp does this by looking up its order table and rather checking if
  9.  
  10. - $a has a smaller (or equal) order in which case the loop exits early with return 0 or if
  11. - $b has a smaller order in which case the loop exits early with return 1.
  12.  
  13. Be aware that this is wrong according to the PHP Documentation which states it wants positive/negative/0 as return values. This is only correct if you know that the internals only check for comparator($a,$b) > 0, meaning it only checks if $b is smaller than and not equal to $a, making this a comparison order of $a <= order of $b. It can easily break if the code starts to check for $a being smaller than and not equal to $b.
  14.  
  15. 2) How does the quicksort sorting work?
  16.  
  17. For starters I'm assuming you are using PHP 7 or higher. In that case you hit a special case with 6-15 Element sized arrays. PHP 7+ does not seem to use quicksort for short lists, instead it uses an Insertion Sort Variant (which is most probably faster due to Hardware related stuff like caching/code locality). You can check the sorting source code f.e. on the Github PHP Mirror (as an example: PHP 7.0 Zend/Zend_sort.c Line 177-198).
  18.  
  19. The code works in 3 steps:
  20.  
  21. 1) comparision: compare neighbor elements array[j] and array[j+1], if array[j] <= array[j+1] move on, else goto 2.
  22. 2) find insertion point: now if array[j] > array[j+1], scan backwards to find the point where array[x] < array[j+1] <= array[x+1] for x < j (obviously only until x hits the start)
  23. 3) insert: shift elements x+1 ... j one up such that it becomes x+2 ... j+1 and insert former element at position x+1
  24.  
  25. If you apply that code to the pairings (2-1, 2-3, 1-3, 2-4, 3-4, 2-2, 2-1, 2-1, 4-1, 3-1, 1-1, 2-2) it gets obvious what the code does.
  26.  
  27. -- [2,1],3,4,2,1,2 -> 1./2./3. compare [2,1], find and insert 1 before 2
  28. -- 1,[2,3],4,2,1,2 -> 1./2. compare [2,3], find insert point for 3 (since order of 3 < order of 2)
  29. -- [1,3],2,4,2,1,2 -> 3. compare [1,3], found insert point for 3 before 2
  30. -- 1,3,[2,4],2,1,2 -> 1./2. compare [2,4], find insert point for 4 (since order of 4 < order of 2)
  31. -- 1,[3,4],2,2,1,2 -> 3. compare [3,4], found insert point for 4 before 2
  32. -- 1,3,4,[2,2],1,2 -> 1. compare [2,2], skip
  33. -- 1,3,4,2,[2,1],2 -> 1./2. compare [2,1], find insert point for 1
  34. -- 1,3,4,[2,1],2,2 -> 2. compare [2,1], find insert point for 1
  35. -- 1,3,[4,1],2,2,2 -> 2. compare [4,1], find insert point for 1
  36. -- 1,[3,1],4,2,2,2 -> 2. compare [3,1], find insert point for 1
  37. -- [1,1],3,4,2,2,2 -> 3. compare [1,1], fond insert point for 1 before 3
  38. -- 1,1,3,4,2,[2,2] -> 1. compare [2,2], skip
  39. -- sorted: 1,1,3,4,2,2,2
  40.  
  41. PS: Here you already see that it is quite complex to derive the work of even a simple sorting algorithm (22 lines of code) by its comparison patterns. The PHP 7 quicksort implementation is around 10 times as big in lines of code and has some freaky optimizations (besides normal madness due to pivot selection and recursions).
  42.  
  43. Most of the times it is better to ignore in depth implementation details and only reduce it to the stuff that is needed. Typical questions for a sorting algorithm would be if it is stable/unstable and performing in O(log n) with O(n) Memory consumption. There are easier ways to learn the core algorithms behind those optimized implementations like the Quicksort Dance or any other visualization or a good old (e)book or webpage with examples.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement