Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 理解mihheap和heapSort方法_Java_Heap_Heapsort - Fatal编程技术网

Java 理解mihheap和heapSort方法

Java 理解mihheap和heapSort方法,java,heap,heapsort,Java,Heap,Heapsort,我有这两个类,我正在询问heapSort()方法。如何在Testcalss中实现它,另外,您可以检查我的add()方法和我的smallestChild()方法吗 对于heapSort()方法,该方法应按升序对数组进行排序算法是遍历数组并将所有数字添加到数组中,然后删除所有数字并将其放回数组中!!老实说,这个算法把我弄糊涂了,我不知道该怎么做?heapSort()是否需要助手方法?或者如何 这是第一个类MinHeap import java.util.NoSuchElementException;

我有这两个类,我正在询问
heapSort()
方法。如何在
Test
calss中实现它,另外,您可以检查我的
add()
方法和我的
smallestChild()
方法吗

对于
heapSort()
方法,该方法应按升序对数组进行排序算法是遍历数组并将所有数字添加到数组中,然后删除所有数字并将其放回数组中!!老实说,这个算法把我弄糊涂了,我不知道该怎么做?
heapSort()
是否需要助手方法?或者如何

这是第一个类
MinHeap

import java.util.NoSuchElementException;
public class MinHeap {
    private int[] heap;   // The heap.
    private int size; // the next index
    public MinHeap(int capacity) {
        heap = new int[capacity];
        size = 0;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void add(int n) {
        if (size < heap.length) {
            size++;
            n = heap[size-1];
            int p = (size -1)/2;
            while(n != 0 && n < heap[p]) {
                swap(size, p);
                size = p;
                p=(size-1)/2;
            }
            size++; 
        } else {
            throw new HeapFullException();
        }
    }

    private int smallestChild(int current) {
        if(size < heap.length) {
            int left = current *2+2;
            int right = current * 2 +1;
            if(heap[left]>heap[right]) {
                return left;
            } else if(heap[right]>heap[left]) {
                return right;
            } else {
                return left;
            }
        } else {
            return -1;
        }       
    }

    public int remove() {
        if (size == 0) {
            // if size has no element to remove throw exception
            throw new NoSuchElementException();
        } else {
            // hold the minimum element
            int minElement = heap[0];
            // set the minimum index to the highest index and decrement the size
            heap[0] = heap[size-1];
            size--;
            int p = 0;
               while(heap[p] > heap[smallestChild(p)]) {
                int c = smallestChild(p);
                swap(p, c);
                p = c;
            }
            return minElement;
        }
    }

    public String toString() {
        String s = "";
        for(int i=0; i<size; i++) {
            s += i + ": " + heap[i] + "\n";
        }
        return s;
    }

    public int[] toArray() {
        int[] a = new int[size];
        for(int i=0; i<size; i++) {
            a[i] = heap[i];
        }
        return a;
    }

    private void swap(int x, int y) {
        int temp = heap[x];
        heap[x] = heap[y];
        heap[y] = temp;
    }

    class HeapFullException extends RuntimeException {
        public static final long serialVersionUID = 8320632366082135L;
    }
}
如何编写
sortHeap()
?? 请帮助我理解它,为我的考试做好准备
谢谢

您的
add
方法没有向堆中添加任何内容;没有将
n
的值放入数组的代码。而用来在堆中筛选东西的代码是不正确的。
add
的正确版本为:

public void add(int n) {
    if (size >= heap.length) {
        throw new HeapFullException();
    }

    // place the item in the heap
    int pos = size;
    heap[pos] = n;
    size++;

    // move the item into position
    int parent = (pos-1)/2;
    while (pos > 0 && heap[parent] > heap[pos]) {
        swap(parent, pos);
        pos = parent;
        parent = (pos-1)/2;
    }
}
您的
smallestChild
方法有几个bug

private int smallestChild(int current) {
    if(size < heap.length) {
        int left = current *2+2;
        int right = current * 2 +1;
        if(heap[left]>heap[right]) {
            return left;
        } else if(heap[right]>heap[left]) {
            return right;
        } else {
            return left;
        }
    } else {
        return -1;
    }       
}
对于您的
sortHeap
方法,我认为您需要如下内容:

public static void testMinHeap(){
    int[] a = initRandom();
    MinHeap h = new MinHeap(a.length);
    for(int i=0; i<a.length; i++) {
        h.add(a[i]);
    }

    // now remove things in order and add them back to the array
    int ix = 0;
    while (!h.isEmpty) {
      a[ix] = h.remove();
      ix++;
    }

    print(a);
}
publicstaticvoidtestminheap(){
int[]a=initRandom();
MinHeap h=新的MinHeap(a.长度);

对于(int i=0;i@3noo6如果我的答案解决了你的问题,你可以考虑把它标记为被接受的答案。
private int smallestChild(int current) {
    int left = current*2+1;
    if (left >= size) {
        return current;
    }
    int smallest = left;
    int right = left+1;
    if (right < size && heap[right] < heap[left]) {
        smallest = right;
    }
    return smallest;
}
public static void testMinHeap(){
    int[] a = initRandom();
    MinHeap h = new MinHeap(a.length);
    for(int i=0; i<a.length; i++) {
        h.add(a[i]);
    }

    // now remove things in order and add them back to the array
    int ix = 0;
    while (!h.isEmpty) {
      a[ix] = h.remove();
      ix++;
    }

    print(a);
}