Thread: How to read heapsort results

  1. #1
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200

    How to read heapsort results

    Hello,


    I am studying heapsort algorithm now and I don't understand how to read final stage on it.
    Here is an example, the graph shown bellow presents the final stage of heapsort. Now let's say I want to put values one by one into array...hmm looks like I have to implement reading algorithm o.0
    Reading from 1 to 10 doesn't work because array[16,14,10,8,7,9,fail..]
    the same nonsense in descending order ;/
    How to read heapsort results-heapsort_finished-png


    So, where I understand it incorrectly or what I have missed ?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MartinR
    Reading from 1 to 10 doesn't work
    But of course: that is not how heap sort works. If you take a quick look at the DADS entry for heapsort: "A sort algorithm that builds a heap, then repeatedly extracts the maximum item."
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    laserlight, you are right I misunderstood the algorithm's behavior. Now I am going to implement it, btw the book I read shows algorithms comparison table in which quicksort is at best case as fast as heap sort but is definitely slower than qsort in worst case. Then they says that well implement quick sort beats heapsort. Hmm how is this possible if quick sort best case performance is just as good as heapsort ?


    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MartinR
    the book I read shows algorithms comparison table in which quicksort is at best case as fast as heap sort but is definitely slower than qsort in worst case. Then they says that well implement quick sort beats heapsort. Hmm how is this possible if quick sort best case performance is just as good as heapsort ?
    If you are concerned about best case time complexity for a comparison-based sorting algorithm, then you can always make a single pass before starting the sort proper to check if the sequence is sorted, upon which you have a best case time complexity of O(n).

    So, perhaps what you find more strange is that the average case time complexity for heapsort and quicksort is the same, yet in practice quicksort tends to beat heapsort. For this, recall that the Big-O notation in which the time complexity is expressed leaves out constant terms, yet these terms can matter, e.g., if you always make one pass over the sequence, the time complexity is O(n), but if you always make 1000 passes over the sequence, the time complexity is still O(n), yet it is reasonable to expect the former to beat the latter for the same input if the work done on each pass is roughly the same.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    laserlight, yes I forgot about those constants, you are right thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heapsort
    By kornstalk in forum C Programming
    Replies: 7
    Last Post: 12-07-2014, 11:52 PM
  2. Heapsort
    By myle in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2008, 01:44 AM
  3. heapsort help please!
    By MAC77 in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2005, 12:28 PM
  4. Heapsort
    By abalfazl in forum C Programming
    Replies: 2
    Last Post: 08-06-2005, 06:11 PM
  5. heapsort help plz
    By nsssn73 in forum C# Programming
    Replies: 0
    Last Post: 06-02-2002, 06:54 AM