Why is mergesort considered "the way to go" when sorting lists and not quicksort? I heard this in a lecture that I watched online, and saw it in a couple of w开发者_StackOverflow社区ebsites.
One of the main sources of efficiency in quicksort is locality of reference, where the computer hardware is optimized so that accessing memory locations that are near one another tends to be faster than accessing memory locations scattered throughout memory. The partitioning step in quicksort typically has excellent locality, since it accesses consecutive array elements near the front and the back. As a result, quicksort tends to perform much better than other sorting algorithms like heapsort even though it often does roughly the same number of comparisons and swaps, since in the case of heapsort the accesses are more scattered.
Additionally, quicksort is typically much faster than other sorting algorithms because it operates in-place, without needing to create any auxiliary arrays to hold temporary values. Compared to something like merge sort, this can be a huge advantage because the time required to allocate and deallocate the auxiliary arrays can be noticeable. Operating in-place also improves quicksort's locality.
When working with linked lists, neither of these advantages necessarily applies. Because linked list cells are often scattered throughout memory, there is no locality bonus to accessing adjacent linked list cells. Consequently, one of quicksort's huge performance advantages is eaten up. Similarly, the benefits of working in-place no longer apply, since merge sort's linked list algorithm doesn't need any extra auxiliary storage space.
That said, quicksort is still very fast on linked lists. Merge sort just tends to be faster because it more evenly splits the lists in half and does less work per iteration to do a merge than to do the partitioning step.
Hope this helps!
The cost of find() is more harmful to quicksort than mergesort.
Merge sort performs more "short range" operations on the data, making it more suitable for linked lists, whereas quicksort works better with random access data structure.
It is more memory efficient, that's why. Speed wise, it is slower. Linked lists have to iterate, through each element to get to the desired element, the only elements that can be accessed directly are the first element and the last element, in the case of a doubly linked list and this will make the addition and deletion faster, at the first and last elements of the list, otherwise it will be slower because it will have to iterate through each element to get to the specified index.
Arrays on the other hand are less, memory efficient, because the number of elements that it can hold is fixed and as a result, if not all of the spaces available in an array are not used, then the space is wasted, because when an array is created, the number of elements of the selected data type specified at the declaration will be allocated. Arrays are less efficient in deleting and adding data, because after the data has been added or deleted, the whole array must be re-arranged. On the other hand, arrays can access elements a lot faster, O(1) time complexity to be exact.
So in conclusion, arrays have faster overall search times, the deletion and addition times have a O(m - n + 1) time complexity in all cases, where "m" is the size of the array and "n" is the desired element index. Linked lists have O(1) addition and deletion times at the first and last element, but what is in between the time complexity is is worst, because it has to iterate through each element of the list, to get to that element. Linked lists have the best memory allocation, because linked list can change its size at runtime.
CREDIT: https://stackoverflow.com/a/65286515/16587692
精彩评论