Rick10101221

Mergesort vs Quicksort

Jul 18th, 2022 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. Mergesort
  2. - O(nlogn)
  3. - Preferred over Quicksort for Linked Lists
  4. - Linked nodes may not be adjacent in memory (Quicksort may not take advantage of cache)
  5. - Can insert items in middle of list in O(1) time/space, so merge sort can be implemented without extra space
  6. - Quicksort requires a lot of random accessing in arrays (ie A[i]), which cannot be done in linked lists
  7. - Does fewer comparisons, so may be better when complicated comparison routines are used
  8. - Divide-then-conquer algorithm. The partitioning happens in a trivial way, by splitting the input array in half. Most of the work happens during the recursive calls and the merge phase
  9.  
  10. Quicksort
  11. - O(n^2). Theta(nlogn).
  12. - Preferred over Mergesort for arrays
  13. - Doesn't have to create new arrays for merging (Mergesort has to allocate/deallocate arrays)
  14. - Often faster for smaller arrays, and on arrays of a few distinct values repeated many times
  15. - Conquer-then-divide algorithm, which does most of the work during the partitioning and the recursive calls. The subsequent reassembly of the sorted partitions involves trivial effort
  16. - Considered faster than merge since it is
  17. - in-place (arguable since requires O(n) auxiliary space for call stack)
  18. - cache-friendly (working on same array, so more cache hits than merge - main reason why this is faster)
  19. - tail recursive (can rewrite last recursive call to iterative implementation - Python does not automatically do this for
  20. optimization, but other languages do).
  21.  
  22. Sources:
  23. - https://rosettacode.org/wiki/Sorting_algorithms/Quicksort#:~:text=%22On,phase.
  24. - https://www.geeksforgeeks.org/quick-sort/#:~:text=why%20quick%20sort%20is%20preferred%20over%20mergesort%20for%20sorting%20arrays%20%3F
Add Comment
Please, Sign In to add comment