Java 堆排序堆方法不工作?
我真的在这件事上费了好一阵子的劲,真的很想得到一些答案 我有一个基于数组的堆实现。我在插入时的上堆排序似乎可以工作,但当我弹出最大值时,下堆排序无法工作。我已经将完整的代码放在这里,如果有人能够纠正downHeap方法,使其能够在删除最大值时对该数组进行排序,我将非常高兴:Java 堆排序堆方法不工作?,java,sorting,Java,Sorting,我真的在这件事上费了好一阵子的劲,真的很想得到一些答案 我有一个基于数组的堆实现。我在插入时的上堆排序似乎可以工作,但当我弹出最大值时,下堆排序无法工作。我已经将完整的代码放在这里,如果有人能够纠正downHeap方法,使其能够在删除最大值时对该数组进行排序,我将非常高兴: public class HeapSortArray { static int sizeOfTree = 0; private static int arrayBufferSize = 50; publ
public class HeapSortArray {
static int sizeOfTree = 0;
private static int arrayBufferSize = 50;
public static int[] heap = new int[arrayBufferSize];
static int[] numbers = new int[]{ 0, 7, 9, 7, 5, 2, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
public static void main(String[] args) {
insert(0); //set 0 of the array to nothing
//fill array with numbers
for (int i = 1; i < numbers.length; i++) {
insert(numbers[i]);
if (i > 1) {
upHeap(i);
}
}
System.out.println("Heap Printed once inserted: ");
for(int i=1; i < numbers.length; i++){
System.out.println(heap[i]);
}
System.out.println("----------------------");
System.out.println("Heap Printed as top value is popped out: ");
//pop lowest value
for(int i = numbers.length; i != 1; i--){
removeMin();
}
}
public static void upHeap(int childLocation) {
int parentLocation = childLocation / 2;
int child = childLocation;
int parentTemp = heap[parentLocation];
int childTemp = heap[childLocation];
int parentValue = heap[parentLocation];
int childValue = heap[child];
if (parentValue > childValue) {
heap[parentLocation] = childTemp;
heap[childLocation] = parentTemp;
}
if (parentLocation != 1) {
upHeap(parentLocation);
}
}
public static void downHeap(int location){
if(location > 0){
int parentLocation = location;
int leftchildLocation = 2*location;
int rightchildLocation = 2*location+1;
int parentValue = heap[parentLocation];
int leftchildValue = heap[leftchildLocation];
int rightchildValue = heap[rightchildLocation];
int parentTemp = heap[parentLocation];
int leftChildTemp = heap[leftchildLocation];
int rightChildTemp = heap[rightchildLocation];
if(leftchildValue < rightchildValue && leftchildValue < parentValue){
heap[parentLocation] = leftchildValue;
heap[leftchildLocation] = parentTemp;
downHeap(leftchildLocation);
}
if(rightchildValue < leftchildValue && rightchildValue < parentValue){
heap[parentLocation] = rightchildValue;
heap[rightchildLocation] = parentTemp;
downHeap(rightchildLocation);
}
}
}
public static int removeMin() {
sizeOfTree--;
if(sizeOfTree > 1){
heap[1] = heap[sizeOfTree];
downHeap(1);
}
int toReturn = heap[1];
System.out.println(toReturn);
return toReturn;
}
public static void insert(int toInsert) {
heap[sizeOfTree] = toInsert;
if(sizeOfTree > 1){
upHeap(sizeOfTree);
}
sizeOfTree++;
}
}
更改为:
int parentLocation = childLocation -1;
我知道第二种方法并不是它的本意,但是为什么我的childLocation/2没有给我应该给我的父母呢 以下是下堆中必需的检查:
public static void downHeap(int location){
if(location < sizeOfTree){
int p = location;
int l = 2*p;
int r = 2*p+1;
int s = sizeOfTree;
if(r<s && heap[r]<heap[p] && heap[r]<heap[l]){
int temp = heap[r];
heap[r] = heap[p];
heap[p] = temp;
downHeap(r);
}else if(l<s && heap[l]<heap[p]){
int temp = heap[l];
heap[l] = heap[p];
heap[p] = temp;
downHeap(l);
}
}}
以下是下堆中必需的检查:
public static void downHeap(int location){
if(location < sizeOfTree){
int p = location;
int l = 2*p;
int r = 2*p+1;
int s = sizeOfTree;
if(r<s && heap[r]<heap[p] && heap[r]<heap[l]){
int temp = heap[r];
heap[r] = heap[p];
heap[p] = temp;
downHeap(r);
}else if(l<s && heap[l]<heap[p]){
int temp = heap[l];
heap[l] = heap[p];
heap[p] = temp;
downHeap(l);
}
}}
PriorityQueue
是否提供了您需要的功能,或者您实现自己的功能还有其他原因吗?这正是我想要的,但只是通过使用数组。PriorityQueue
是否提供了您需要的功能,或者你实现你自己的问题还有其他原因吗?这就是我想要的,但只是通过使用数组。谢谢Wyn07,你能看一下对我主要问题的编辑吗?谢谢Wyn07,你能看一下对我主要问题的编辑吗?
public static int removeMin() {
sizeOfTree--;
int toReturn = heap[1];
if(sizeOfTree > 1){
heap[1] = heap[sizeOfTree];
downHeap(1);
}
System.out.println(toReturn);
return toReturn;
}