How do I improve my Python code about heap sort?

Nicholas

I tried to write a heap sort program myself for Leetcode 217. Contains Duplicate as below rather than using Python built-in sort method. Leetcode should accept heap sort method, but for some reasons I don't know, although my heap sort program works well, I still got the runtime rejection from Leetcode. Can anyone help?

Solved, below code is re-edited using Floyd algorithm to initialize heap and has passed Leetcode

def heapsort(nums):

    def swap(i, j):

        nums[i], nums[j] = nums[j], nums[i]


    def sift(start, size):

        l = (start << 1) + 1 # Do not forget () for <<
        r = l + 1
        largest = start
        if l <= size - 1 and nums[start] < nums[l]:
            largest = l
        if r <= size - 1 and nums[largest] < nums[r]:
            largest = r   
        if largest != start:
            swap(start, largest)
            sift(largest, size)


    size = len(nums)

    # Initialize heap (Floyd Algorithm)
    end = (size >> 1) - 1
    while end >= 0:
        sift(end, size)
        end -= 1
    swap(0, size - 1)
    size -= 1

    # Heapify recursively
    while size > 1:
        sift(0, size)
        swap(0, size - 1)
        size -= 1
Jim Mischel

Your code does way too much. You're rebuilding the entire heap with every item that you remove. So what should be an O(n log n) algorithm is instead O(n^2).

Essentially, your code is doing this:

while array is not empty
    rearrange array into a heap
    extract the smallest item

Rearranging the heap takes, at best, O(n) time. And extracting the smallest takes O(log n). So your algorithm is O(n^2 + n log n).

Actually, your method of building the heap from the bottom up is O(n log n) in itself. So your heap sort algorithm is actually O((n+1)*(n log n)). In any case, it's a highly sub-optimal algorithm.

The idea behind heap sort is that you arrange the array into a heap one time. That is an O(n) operation. The algorithm is pretty simple:

for i = heap.length/2 downto 1
    siftDown(i)

This is called Floyd's algorithm, after its inventor.

Note that we start in the middle of the array and sift down. The idea is that the last n/2 items are leaf nodes, so they can't sift down, anyway. By starting at n/2 and working backwards, we can heapify the entire array in O(n) time.

After the array is arranged into a heap, we do the following:

while heap is not empty
    output the item at heap[0]
    move the item at the end of the heap to heap[0]
    reduce the count of items by 1
    siftDown(0)

The item at heap[0] is the smallest item remaining in the heap, so we output it. Then, there's no need to rebuild the entire heap. All you have to do is take the last item in the heap, place it at the top, and sift it down into position. The rest of the heap remains valid.

Making those changes should reduce the running time, although I don't know if that's going to make your code acceptable. There is another way to check for duplicates. It requires O(n) extra space, but it's faster than sorting.

The idea is to create a hash table and then go through the array, checking if the item is in the hash table. If not, add it. If it's already in the table, then it's a duplicate. As Harold pointed out, Python has a set type that makes this kind of thing easy to do.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

One of the method from my python code fails for some unittests. How do I improve it?

From Dev

How do i improve my existing code for GetOpenfile function

From Dev

How can i improve my python code regarding while loops

From Dev

How do I improve the performance of the following code in python

From Dev

How do i improve my code to load images from remote server more effiecintly without UI lag?

From Dev

How can I improve this code in python?

From Dev

How can I improve my code to reduce the synthesis time?

From Dev

How can I improve my webpage code to be scalable for smaller sizes?

From Dev

How can I improve my SQL code for correct results?

From Dev

How can I improve my code to reduce the synthesis time?

From Dev

How can I improve my SQL code for correct results?

From Dev

How can I improve my code to handle large numbers?

From Dev

How can I improve my "if and else if" VBA? code

From Dev

Can someone improve my heap data structure code?

From Dev

How do I improve my Neural Network output?

From Dev

How do I improve the performance of my VirtualBox guest?

From Dev

How do I improve the quality of my FFsplit Twitch stream?

From Dev

How do I improve the performance of my VirtualBox guest?

From Dev

How do I add a peek function to my heap Class?

From Dev

How do I run unit tests on my Python code in Buildout?

From Dev

How do I insert this python code into my html template?

From Dev

How do I send the prompt backwards in my code in python?

From Dev

How do I go about making this part of my code reactive? (Meteor + React)

From Dev

How can i improve my multithreading speed and effciency in python?

From Dev

How can I improve the performance of the below python code

From Dev

How do I programmatically know when I’m about to exceed the javascript heap size?

From Dev

How to improve the performance of this Python code?

From Dev

How to improve Python code speed

From Dev

How to improve the performance of this Python code?

Related Related

  1. 1

    One of the method from my python code fails for some unittests. How do I improve it?

  2. 2

    How do i improve my existing code for GetOpenfile function

  3. 3

    How can i improve my python code regarding while loops

  4. 4

    How do I improve the performance of the following code in python

  5. 5

    How do i improve my code to load images from remote server more effiecintly without UI lag?

  6. 6

    How can I improve this code in python?

  7. 7

    How can I improve my code to reduce the synthesis time?

  8. 8

    How can I improve my webpage code to be scalable for smaller sizes?

  9. 9

    How can I improve my SQL code for correct results?

  10. 10

    How can I improve my code to reduce the synthesis time?

  11. 11

    How can I improve my SQL code for correct results?

  12. 12

    How can I improve my code to handle large numbers?

  13. 13

    How can I improve my "if and else if" VBA? code

  14. 14

    Can someone improve my heap data structure code?

  15. 15

    How do I improve my Neural Network output?

  16. 16

    How do I improve the performance of my VirtualBox guest?

  17. 17

    How do I improve the quality of my FFsplit Twitch stream?

  18. 18

    How do I improve the performance of my VirtualBox guest?

  19. 19

    How do I add a peek function to my heap Class?

  20. 20

    How do I run unit tests on my Python code in Buildout?

  21. 21

    How do I insert this python code into my html template?

  22. 22

    How do I send the prompt backwards in my code in python?

  23. 23

    How do I go about making this part of my code reactive? (Meteor + React)

  24. 24

    How can i improve my multithreading speed and effciency in python?

  25. 25

    How can I improve the performance of the below python code

  26. 26

    How do I programmatically know when I’m about to exceed the javascript heap size?

  27. 27

    How to improve the performance of this Python code?

  28. 28

    How to improve Python code speed

  29. 29

    How to improve the performance of this Python code?

HotTag

Archive