heap,heapsort,min-heap,max-heap

Just flip the sign of all your values before you put them into the heap, and again as you take them out of the heap.

java,arrays,sorting,quicksort,heapsort

So after few weeks of pain I managed to understand what was wrong. Everything seems to work fine with the following moveDownAux method: private static <T extends Comparable<? super T>> void moveDownAux(T[] a, int min, int i, int max) { T e = a[i]; int j; while((j = (2*i)+1 -...

There's definitely a problem with the implementation of heap sort. Looking at hesorti, you can see that it is just reversing the elements of the vector after calling build_max_heap. So somehow build_max_heap isn't just making a heap, it's actually reverse sorting the whole array. max_heapify already has an issue: in...

Your index calculations are off: int left = 2*i; int right = 2*i + 1; If i is 0, then we want left and right to be 1 and 2. If i is 1, then left and right should be 3 and 4, and so on. The calculations should be:...

The problem is with max = Li[0].extractMax(); You are not checking if Li[0] might be empty. Always check preconditions and fail fast. The problem would have become immediately obvious had you started extractMax and deleteMax with if (_size == 0) { throw new IllegalStateException("empty heap"); } Here's the fixed final...

Errors contains in function Heapify. The corrected version of the function: private void Heapify(int[] arr, int index) { int left = 2 * index + 1; int right = 2 * index + 2; int largest = index; if (left <= heapSize && arr[left] > arr[index]) { largest = left;...

arrays,algorithm,sorting,heapsort

After, some time spent on searching I found why there is a heap-size, and also an array.length (the array is actually the object where the heap is represented). If you don't want actually to have more free spots at the array tail and when you add an element in the...

Note that the code is in complete: it lacks a main() and the structure maxHeap, i.e., it takes unnecessary effort to even reproduce the problem. The code clearly doesn't have any debugging indication and single-stepping through the code is probably fairly pointless. The way to find the problem is to...

Your code accesses the element arr[size] several times, which is one item beyond the valid range, 0 <= index < size. In particular: if ((left<=size)&&(arr[left]>arr[i])) max = left; else max = i; if ((right<=size)&&(arr[right]>arr[max])) max = right; Here, you should replace all ... <= size with ... < size. int...

c++,arrays,algorithm,heap,heapsort

You are reading out of bounds. When you check: if((c < size) where c is the last element, you read out of bounds here: if((c < size) && (wordArray[c] < wordArray[c+1])){ ^ The check should be: if((c+1 < size) And yet another problem is here: while(r <= size) where r...