Java 在用户已经创建堆之后,如何使用HeapSort方法?
嘿,伙计们,我正在为我的编程课做一个实验作业,我们必须创建一个堆,用户在其中输入整数到数组中,然后显示它,然后我们假设使用这些相同的值并使用HeapSort。第一部分相当简单,每次遇到这个错误时,我都无法调用HeapSort方法来对数组进行排序 线程“main”java.lang.NullPointerException中出现异常 HeapApp.heapSort(HeapApp.java:11)Java 在用户已经创建堆之后,如何使用HeapSort方法?,java,algorithm,heapsort,binary-heap,Java,Algorithm,Heapsort,Binary Heap,嘿,伙计们,我正在为我的编程课做一个实验作业,我们必须创建一个堆,用户在其中输入整数到数组中,然后显示它,然后我们假设使用这些相同的值并使用HeapSort。第一部分相当简单,每次遇到这个错误时,我都无法调用HeapSort方法来对数组进行排序 线程“main”java.lang.NullPointerException中出现异常 HeapApp.heapSort(HeapApp.java:11)at HeapApp.main(HeapApp.java:88) 这个错误特别指出了这一点 int
at HeapApp.main(HeapApp.java:88)
这个错误特别指出了这一点
int count = hp.length;
及
请帮忙!这是我这门课最后的作业之一
堆类
import java.util.ArrayList;
import java.util.NoSuchElementException;
public class Heap<T extends Comparable<T>> {
private ArrayList<T> items;
public Heap() {
items = new ArrayList<T>();
}
private void siftUp() {
int k = items.size() - 1;
while (k > 0) {
int p = (k-1)/2;
T item = items.get(k);
T parent = items.get(p);
if (item.compareTo(parent) > 0) {
// swap
items.set(k, parent);
items.set(p, item);
// move up one level
k = p;
} else {
break;
}
}
}
public void insert(T item) {
items.add(item);
siftUp();
}
private void siftDown() {
int k = 0;
int l = 2*k+1;
while (l < items.size()) {
int max=l, r=l+1;
if (r < items.size()) { // there is a right child
if (items.get(r).compareTo(items.get(l)) > 0) {
max++;
}
}
if (items.get(k).compareTo(items.get(max)) < 0) {
// switch
T temp = items.get(k);
items.set(k, items.get(max));
items.set(max, temp);
k = max;
l = 2*k+1;
} else {
break;
}
}
}
public T delete()
throws NoSuchElementException {
if (items.size() == 0) {
throw new NoSuchElementException();
}
if (items.size() == 1) {
return items.remove(0);
}
T hold = items.get(0);
items.set(0, items.remove(items.size()-1));
siftDown();
return hold;
}
public int size() {
return items.size();
}
public boolean isEmpty() {
return items.isEmpty();
}
public String toString() {
return items.toString();
}
}
import java.util.Scanner;
public class HeapApp {
/**
* @param args
*/
public static void main(String[] args) {
Heap<Integer> hp = new Heap<Integer>();
Scanner sc = new Scanner(System.in);
HeapApp HP = new HeapApp();
System.out.print("Enter next int, 'done' to stop: ");
String line = sc.next();
while (!line.equals("done")) {
hp.insert(Integer.parseInt(line));
System.out.println(hp);
System.out.print("Enter next int, 'done' to stop: ");
line = sc.next();
}
while (hp.isEmpty()) {
//int max = hp.delete();
System.out.println(hp);
}
System.out.println(hp);
HP.heapSort(HP);
System.out.println("After sorting " + hp);
}
private static int [] hp;
public static void heapSort(HeapApp HP){
int count = hp.length;
//first place a in max-heap order
heapify(hp, count);
int end = count - 1;
while(end > 0){
//swap the root(maximum value) of the heap with the
//last element of the heap
int tmp = hp[end];
hp[end] = hp[0];
hp[0] = tmp;
//put the heap back in max-heap order
siftDown(hp, 0, end - 1);
//decrement the size of the heap so that the previous
//max value will stay in its proper place
end--;
}
}
public static void heapify(int[] hp, int count){
//start is assigned the index in a of the last parent node
int start = (count - 2) / 2; //binary heap
while(start >= 0){
//sift down the node at index start to the proper place
//such that all nodes below the start index are in heap
//order
siftDown(hp, start, count - 1);
start--;
}
//after sifting down the root all nodes/elements are in heap order
}
public static void siftDown(int[] hp, int start, int end){
//end represents the limit of how far down the heap to sift
int root = start;
while((root * 2 + 1) <= end){ //While the root has at least one child
int child = root * 2 + 1; //root*2+1 points to the left child
//if the child has a sibling and the child's value is less than its sibling's...
if(child + 1 <= end && hp[child] < hp[child + 1])
child = child + 1; //... then point to the right child instead
if(hp[root] < hp[child]){ //out of max-heap order
int tmp = hp[root];
hp[root] = hp[child];
hp[child] = tmp;
root = child; //repeat to continue sifting down the child now
}else
return;
}
}
}
import java.util.ArrayList;
导入java.util.NoSuchElementException;
公共类堆{
私有ArrayList项;
公共堆(){
items=newarraylist();
}
私有void siftUp(){
int k=items.size()-1;
而(k>0){
int p=(k-1)/2;
T item=items.get(k);
T parent=items.get(p);
如果(项目比较(父项)>0){
//交换
项目集(k,父项);
项目集(p,项目);
//升一级
k=p;
}否则{
打破
}
}
}
公共无效插入(T项){
项目。添加(项目);
siftUp();
}
私人空间{
int k=0;
int l=2*k+1;
而(l0){
max++;
}
}
if(items.get(k).compareTo(items.get(max))<0{
//开关
T temp=项目获取(k);
items.set(k,items.get(max));
项目设置(最大值、温度);
k=最大值;
l=2*k+1;
}否则{
打破
}
}
}
公营部门不删除()
抛出非接触元素异常{
如果(items.size()==0){
抛出新的NoTouchElementException();
}
如果(items.size()==1){
返回项目。删除(0);
}
T hold=items.get(0);
items.set(0,items.remove(items.size()-1));
siftDown();
返回保持;
}
公共整数大小(){
返回items.size();
}
公共布尔值为空(){
return items.isEmpty();
}
公共字符串toString(){
return items.toString();
}
}
导入java.util.Scanner;
公共类堆{
/**
*@param args
*/
公共静态void main(字符串[]args){
Heap hp=新堆();
扫描仪sc=新的扫描仪(System.in);
HeapApp HP=新的HeapApp();
System.out.print(“输入下一个整数,'done'停止:”);
字符串行=sc.next();
而(!line.equals(“done”)){
hp.insert(Integer.parseInt(line));
系统输出打印项次(hp);
System.out.print(“输入下一个整数,'done'停止:”);
行=sc.next();
}
while(hp.isEmpty()){
//int max=hp.delete();
系统输出打印项次(hp);
}
系统输出打印项次(hp);
heapSort(HP);
System.out.println(“排序后”+hp);
}
私有静态int[]hp;
公共静态无效堆出口(堆出口){
int count=hp.length;
//首先按最大堆顺序放置a
heapify(hp,count);
int end=计数-1;
而(结束>0){
//将堆的根(最大值)与
//堆的最后一个元素
int tmp=马力[结束];
hp[end]=hp[0];
hp[0]=tmp;
//将堆放回最大堆顺序
siftDown(hp,0,end-1);
//减小堆的大小,使上一个
//最大值将保留在其适当的位置
结束--;
}
}
公共静态无效heapify(int[]hp,int count){
//在最后一个父节点的
int start=(计数-2)/2;//二进制堆
while(开始>=0){
//在索引开始处将节点筛选到适当的位置
//这样,开始索引下面的所有节点都在堆中
//命令
siftDown(hp,启动,计数-1);
开始--;
}
//筛选根之后,所有节点/元素都按堆顺序排列
}
公共静态无效siftDown(int[]马力,int开始,int结束){
//end表示要筛选的堆下距离的限制
int root=start;
而((根*2+1)
在访问此线路之前,请验证是否已初始化hp阵列
int count = hp.length;
请参考链接
如果您想使用动态数组,请参阅此处
在访问此线路之前,请验证是否已初始化hp阵列
int count = hp.length;
请参考链接
如果您想使用动态数组,请参阅此处
这里是一个堆排序的示例程序。该示例将int[]作为堆排序的输入。调用doHeapSort方法并传递int[]
public static void doHeapSort(int [] inputArray)
{
for(int i = 0; i < inputArray.length; i++)
{
keepMaxHeapFindingParentElement(i, inputArray);
}
sortAndMaintainHeap(inputArray, inputArray.length - 1);
}
private static void sortAndMaintainHeap(int [] inputArray, int lastElementIndex)
{
if(lastElementIndex <= 0)
{
return;
}
swap(inputArray, 0, lastElementIndex);
lastElementIndex--;
keepMaxHeapFindingChildElement(inputArray, 0, lastElementIndex);
sortAndMaintainHeap(inputArray, lastElementIndex);
}
private static void keepMaxHeapFindingChildElement(int [] inputHeap, int currentElementIndex, int lastElementIndex)
{
if(currentElementIndex >= lastElementIndex)
{
//no more child node
return;
}
int child1Index = 2*currentElementIndex + 1;
int child2Index = 2*currentElementIndex + 2;
int childIndex = 0;
if(child2Index <= lastElementIndex)
{
childIndex = inputHeap[child1Index] > inputHeap[child2Index] ? child1Index : child2Index;
}
else if(child1Index <= lastElementIndex)
{
childIndex = child1Index;
}
else
{
return;
}
if(inputHeap[currentElementIndex] < inputHeap[childIndex])
{
swap(inputHeap, currentElementIndex, childIndex);
keepMaxHeapFindingChildElement(inputHeap, childIndex, lastElementIndex);
}
else
{
return;
}
}
private static void keepMaxHeapFindingParentElement(int elementIndex, int [] inputHeap)
{
if(elementIndex == 0)
{
// no more parent node
return;
}
int parentElementIndex = (elementIndex - 1)/2;
if(inputHeap[elementIndex] > inputHeap[parentElementIndex])
{
//swap child and parent
swap(inputHeap, elementIndex, parentElementIndex);
keepMaxHeapFindingParentElement(parentElementIndex, inputHeap);
}
}
publicstaticvoiddoheapsort(int[]inputArray)
{
for(int i=0;i
这里是一个堆排序的示例程序。该示例使用int[]作为堆排序的输入。调用doheap
public static void doHeapSort(int [] inputArray)
{
for(int i = 0; i < inputArray.length; i++)
{
keepMaxHeapFindingParentElement(i, inputArray);
}
sortAndMaintainHeap(inputArray, inputArray.length - 1);
}
private static void sortAndMaintainHeap(int [] inputArray, int lastElementIndex)
{
if(lastElementIndex <= 0)
{
return;
}
swap(inputArray, 0, lastElementIndex);
lastElementIndex--;
keepMaxHeapFindingChildElement(inputArray, 0, lastElementIndex);
sortAndMaintainHeap(inputArray, lastElementIndex);
}
private static void keepMaxHeapFindingChildElement(int [] inputHeap, int currentElementIndex, int lastElementIndex)
{
if(currentElementIndex >= lastElementIndex)
{
//no more child node
return;
}
int child1Index = 2*currentElementIndex + 1;
int child2Index = 2*currentElementIndex + 2;
int childIndex = 0;
if(child2Index <= lastElementIndex)
{
childIndex = inputHeap[child1Index] > inputHeap[child2Index] ? child1Index : child2Index;
}
else if(child1Index <= lastElementIndex)
{
childIndex = child1Index;
}
else
{
return;
}
if(inputHeap[currentElementIndex] < inputHeap[childIndex])
{
swap(inputHeap, currentElementIndex, childIndex);
keepMaxHeapFindingChildElement(inputHeap, childIndex, lastElementIndex);
}
else
{
return;
}
}
private static void keepMaxHeapFindingParentElement(int elementIndex, int [] inputHeap)
{
if(elementIndex == 0)
{
// no more parent node
return;
}
int parentElementIndex = (elementIndex - 1)/2;
if(inputHeap[elementIndex] > inputHeap[parentElementIndex])
{
//swap child and parent
swap(inputHeap, elementIndex, parentElementIndex);
keepMaxHeapFindingParentElement(parentElementIndex, inputHeap);
}
}