heapsort

Optimizing heap structure for heapsort

依然范特西╮ 提交于 2019-12-12 02:32:45
问题 I'm implementing heapsort using a heap. To do this each value to be sorted is inserted. The insertion method calls heapifyUp() (aka siftUp) so this means each time another value is inserted heapifyUp is called. Is this the most efficient way? Another idea would be to insert all elements, and then call heapifyUp. I guess heapifyUp would have to be called on each one? Is doing it like this better? 回答1: Inserting each element will build the heap in O(n log n) time. Same thing if you add all the

Heap sort pseudo code algorithm

此生再无相见时 提交于 2019-12-11 16:06:53
问题 In heap sort algorithm n=m for k:= m div 2 down to 0 downheap(k); repeat t:=a[0] a[0]:=a[n-1] a[n-1]:=t n— downheap(0); until n <= 0 Can some one please explain to me what is done in lines n=m for k:= m div 2 down to 0 downheap(k); I think that is the heap building process but what is mean by for k:= m div 2 down to 0 Also is n the number of items.So in an array representation last element is stored at a[n-1]? But why do it for n> = 0. Can't we finish at n>0.Because the first element gets

questions Inserting in heapsort

痴心易碎 提交于 2019-12-11 07:58:28
问题 import java.util.ArrayList; import java.util.Collection; public class MaxHeap { private ArrayList<Student> students; public MaxHeap(int capacity) { students = new ArrayList<Student>(capacity); } public MaxHeap(Collection<Student> collection) { students = new ArrayList<Student>(collection); for(int i = size()/2; i >= 0; i--) { maxHeapify(i); } } public Student getMax() { if(size() < 1) { throw new IndexOutOfBoundsException("No maximum value: the heap is empty."); } return students.get(0); }

How to write a sortRemove method for MaxHeapPriorityQueue in Java?

做~自己de王妃 提交于 2019-12-11 07:34:33
问题 I am working on a helper method private E sortRemove(), for my HeapSort static method. Let me note, that this heap is a MaxHeapPriorityQueue, which all the elements have a child that has a value that is smaller than the parent. I am trying to Call remove repeatedly until the heap is empty. But make it so that when an element is "removed", it is moved to the end of the array instead of completely evicted from the array. When you are done, voila! The array is sorted. I'm trying to figure out

c generic heapsort

喜夏-厌秋 提交于 2019-12-11 06:48:46
问题 Okay so I need to create a 'generic' heapsort in c and this is what I have so far (I might be missing some closing brackets in code but they just got lost when I moved my code here) void srtheap(void *, size_t, size_t, int (*)(const void *, const void *)); void heapify(void *, size_t, size_t, size_t, int (*)(const void *, const void *)); void srtheap(void *base, size_t nelem, size_t size, int (*compar)(const void *, const void *)) { void *p1, *p2; void *last = base + (size*(nelem-1)); for

What's wrong with my HeapSort code?

烈酒焚心 提交于 2019-12-10 14:17:02
问题 I'm attempting to write a heapsort method in java but it's not working exactly as I want it to: public class HeapSort { private static int n; private static void swap(int[] A, int a, int b) { int tmp = A[a]; A[a] = A[b]; A[b] = tmp; } private static void insert(int[] A, int i) { int left = i * 2; int right = left + 1; int max = i; if (left <= n && A[left] < A[max]){ max = left; } if (right <= n && A[right] > A[max]) { max = right; } if (max != i) { swap(A, i, max); insert(A, max); } } public

Why does heapify swap the top of the heap with the element at the bottom of the heap?

旧巷老猫 提交于 2019-12-06 03:54:19
In a max heap (assuming it's represented by an array), the top of the heap (ie. the largest value in the heap) swaps with the last element in the array (ie. one of the smallest values in the heap), the last element is removed, and then the new top-of-the-heap element swaps with other values to settle back into its proper place. Instead, why isn't the top element just removed and then other elements can "fill in" for the the heap? One of the key properties of a heap is that the underlying binary tree is a complete binary tree (i.e. every level except the last one has to be completely "filled").

Heap sort using linked lists

安稳与你 提交于 2019-12-06 02:39:15
问题 I was wondering if anyone has ever used linked lists to do heap sort and if they have could they provide the code. I have been able to do heapsort using arrays, but trying to do it in linked lists seems unpractical and just a pain in the you know where. I have to implement linked lists for a project Im doing, any help would be greatly appreciated. Also I am using C. 回答1: The answer is "you don't want to implement heap sort on a linked list." Heapsort is a good sorting algorithm because it's O

Why use a flat list in heapsort?

喜夏-厌秋 提交于 2019-12-04 21:18:27
In heapsort , the data is stored in something called a " heap ". Almost all the implementations I've seen use a flat list for the data structure. Can someone explain to me why this is? Why not use nested arrays or an instance of a binary Tree ? Isn't explicit better than implicit? Is it because of implementation difficulties like traversing the structure, or something else? If you want to see how heapsort can be implemented in Python then look no further than the standard library module heapq . Python has both C and Python implementations of heapsort and the heapq module defines the Python

Why is my n log(n) heapsort slower than my n^2 selection sort

蹲街弑〆低调 提交于 2019-12-03 15:38:27
I have implemented two algorithms for sorting elements from highest to lowest. The first one, takes quadratic time on the real RAM model and the second an O(n log(n)) time. The second one uses priority queues to get the reduction. Here are the timings, which are the output of the above program. the first column is the size of the random array of integers the second column is the time in seconds for the O(n^2) technique the third column is the time in seconds for the O(n log(n)) technique 9600 1.92663 7.58865 9800 1.93705 7.67376 10000 2.08647 8.19094 In spite of this great difference in