heapsort

Efficient heaps in purely functional languages

╄→尐↘猪︶ㄣ 提交于 2019-12-03 01:56:40
问题 As an exercise in Haskell, I'm trying to implement heapsort. The heap is usually implemented as an array in imperative languages, but this would be hugely inefficient in purely functional languages. So I've looked at binary heaps, but everything I found so far describes them from an imperative viewpoint and the algorithms presented are hard to translate to a functional setting. How to efficiently implement a heap in a purely functional language such as Haskell? Edit: By efficient I mean it

Efficient heaps in purely functional languages

心不动则不痛 提交于 2019-12-02 15:32:09
As an exercise in Haskell, I'm trying to implement heapsort. The heap is usually implemented as an array in imperative languages, but this would be hugely inefficient in purely functional languages. So I've looked at binary heaps, but everything I found so far describes them from an imperative viewpoint and the algorithms presented are hard to translate to a functional setting. How to efficiently implement a heap in a purely functional language such as Haskell? Edit: By efficient I mean it should still be in O(n*log n), but it doesn't have to beat a C program. Also, I'd like to use purely

An intuitive understanding of heapsort?

让人想犯罪 __ 提交于 2019-12-02 14:10:05
At school we are currently learning sorting algorithms in Java and I got for my homework the Heap Sort. I did my reading, I tried to find out as much as I could, but it seems I just can't grasp the concept. I'm not asking you to write me a Java program, if you could just explain to me as simply as you can how the Heap Sort works. Matt Fellows Right, so basically you take a heap and pull out the first node in the heap - as the first node is guaranteed to be the largest / smallest depending on the direction of sort. The tricky thing is re-balancing / creating the heap in the first place. Two

heapsort input with most and fewest comparisons

我怕爱的太早我们不能终老 提交于 2019-12-02 05:18:26
问题 Hi I've looked around a bit and haven't been able to find any direct discussion of this question. Most seem to cover time complexity and the big O notation. I'm wondering if and how the order of input into a heapsort algorithm will impact the number of comparisons needed to sort the input. For example, take a heapsort algorithm that sorts in ascending order (smallest to largest)....if I input a set of integers already ordered in this way (ascending) how many comparisons would it require

heapsort input with most and fewest comparisons

你。 提交于 2019-12-01 22:58:41
Hi I've looked around a bit and haven't been able to find any direct discussion of this question. Most seem to cover time complexity and the big O notation. I'm wondering if and how the order of input into a heapsort algorithm will impact the number of comparisons needed to sort the input. For example, take a heapsort algorithm that sorts in ascending order (smallest to largest)....if I input a set of integers already ordered in this way (ascending) how many comparisons would it require compared to a set of input that is ordered in a descending manner (largest to smallest)? How about compared

find a heapified array when converting it to a sorted array, the total number of exchanges is maximal possible

妖精的绣舞 提交于 2019-12-01 06:02:32
问题 Inspired by this post, I googled the worst case of heapsort and found this question on cs.stackexchange.com, but the only answer didn't really answer the question, so I decided to dig it out myself. After hours of reasoning and coding, I've solved it. and I think this question belongs better in SO, so I post it up here. The problem is to find a heapified array containing different numbers from 1 to n, such that when converting it to a sorted array, the total number of exchanges in all sifting

Why isn't heapsort stable?

核能气质少年 提交于 2019-11-30 06:15:36
I'm trying to understand why heapsort isn't stable. I've googled this, but haven't found a good, intuitive explanation. I understand the importance of stable sorting - it allows us to sort based on more than one key, which can be very beneficial (i.e., do multiple sortings, each based on a different key. Since every sort will preserve the relative order of elements, previous sortings can add up to give a final list of elements sorted by multiple criteria). However, why wouldn't heapsort preserve this as well? Thanks for your help! The final sequence of the results from heapsort comes from

Why isn't heapsort stable?

社会主义新天地 提交于 2019-11-29 05:06:59
问题 I'm trying to understand why heapsort isn't stable. I've googled this, but haven't found a good, intuitive explanation. I understand the importance of stable sorting - it allows us to sort based on more than one key, which can be very beneficial (i.e., do multiple sortings, each based on a different key. Since every sort will preserve the relative order of elements, previous sortings can add up to give a final list of elements sorted by multiple criteria). However, why wouldn't heapsort

How to create a HeapSort method to an array in Java?

荒凉一梦 提交于 2019-11-28 10:53:17
问题 I know there are countless of HeapSort methods on Stack Overflow, however none necessarily help me with what I am working on. I have somewhat of an idea what a Heap is, I just don't know how to necessarily grab those value and sort them out to store them into an array. Therefore, my instructions read: The static heapSort(Comparable[], int) method should perform an "in-place" sort (from lowest value to highest value) of the array. The second parameter indicates the number of filled elements in

Lower bound on heapsort?

泄露秘密 提交于 2019-11-28 06:58:29
It's well-known that the worst-case runtime for heapsort is Ω(n lg n), but I'm having trouble seeing why this is. In particular, the first step of heapsort (making a max-heap) takes time Θ(n). This is then followed by n heap deletions. I understand why each heap deletion takes time O(lg n); rebalancing the heap involves a bubble-down operation that takes time O(h) in the height of the heap, and h = O(lg n). However, what I don't see is why this second step should take Ω(n lg n). It seems like any individual heap dequeue wouldn't necessarily cause the node moved to the top to bubble all the way