问题
Heap sort can be implemented using linked list and arrays.
What would be the ideal method of doing it-using linked list or arrays?
What is the time complexity to build heap using arrays and linked lists?Is it O(nlogn) for both?
What is the time complexity for deletion?
回答1:
for array, it is O(nlogn). becoz u can easily fetch element at index i. this characteristic makes it easy to fetch each node's parent and left/right child. and the time complexity for deletion is O(lgn).
for linked list, i think it is a different story. it depends how u define the "next" node. as far as i know, it is more complex than using array.
回答2:
Binary Heap
Time complexity in big O notation
Average Worst case
Space O(n) O(n)
Search N/A Operation N/A Operation
Insert O(log n) O(log n)
Delete O(log n) O(log n)
So, Time complexity is the same independent of whether it is using linked lists arrays .
回答3:
Time complexity is the same using either linked lists or arrays, assuming a proper implementation.
I have several implementations of heap sort at my blog. Start here for the traditional array-based version, then follow the links for several linked list versions.
来源:https://stackoverflow.com/questions/14584149/heap-sort-implementation-with-linked-list-and-arrays