Solving Heapsort in Java: Understanding Index Values

  • Context: Comp Sci 
  • Thread starter Thread starter kmr159
  • Start date Start date
  • Tags Tags
    Index Java
Click For Summary

Discussion Overview

The discussion revolves around implementing a Heapsort algorithm in Java, focusing on understanding index values and debugging issues related to sorting. Participants share code snippets and seek assistance in resolving errors encountered during implementation.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant describes their attempt to implement Heapsort and notes issues with index values fluctuating unexpectedly, leading to improper sorting.
  • Another participant suggests debugging by using a small example and checking the computation of the variable treelength, questioning its potential inaccuracies due to floating-point representation.
  • A third participant proposes renaming the heapSort function to nodeSort for clarity and suggests creating a simpler heapSort interface for easier use.
  • A fourth participant recommends using an IDE with debugging capabilities, highlighting the benefits of stepping through code to inspect variable values and improve programming skills.
  • Participants are encouraged to look at other Heapsort implementations available online to gain insights into potential issues in the current code.

Areas of Agreement / Disagreement

There is no consensus on the specific cause of the issues faced in the Heapsort implementation, and multiple suggestions for debugging and code improvement are presented without agreement on a single solution.

Contextual Notes

Participants express uncertainty regarding the calculation of treelength and its impact on the algorithm's behavior. The discussion does not resolve the underlying issues with the implementation.

kmr159
Messages
7
Reaction score
0

Homework Statement


Develop a Heapsort algorithm using java

Homework Equations


The Attempt at a Solution



I have been trying to implement HeapSort in java. Unfortunately I am running into errors. I am printing out the index values and they go up then down and the code does not sort properly. Can you help me understand why the index values decease. Thanks.

This comment explains how I find the final node before switching the values

// log base 2 of (a.length - index) - truncating to get the int value - gives the length of the tree. 2^(treelength) - 1 gives the position of the final node that will be evaluated before the top value is swapped with the bottom value

The full code is here or below

http://pastebin.com/7tvTc4Nb


Code:
	public static void heapSort(double [] a, int node, int index){
		System.out.println("made it");
		int finalnode = 0;
		int treelength = 0;
		treelength = (int)((Math.log(a.length-index))/(Math.log(2)));

		finalnode = Math.pow(2,treelength) - 1;// log base 2 of (a.length - index) - truncating to get the int value - gives the length of the tree. 2^(treelength) - 1 gives the position of the final node that will be evaluated before the top value is swapped with the bottom value
		
		
		System.out.println("THe Finalnode is " + finalnode);
		System.out.println("The index is " + index);
		if(!(node > finalnode-1) && node >= 0 && index < a.length){	
			System.out.println("in");
			boolean switchval = false;
			double holder = 0;
			
			
			if(node == finalnode){
				switchval = true;
			}		
			if(minNode(a,node,index) != -1){
				fixHeap(a,node, index);
			}
			if(switchval){
				System.out.println("Switch");
				holder = a[0];
				a[0] = a[a.length-(1+index)];
				a[a.length-(1+index)] = holder;
				index++;
				heapSort(a,0,index);		
			}
			else{
				heapSort(a,(2*node)	+1,index);
				heapSort(a,(2*node)	+2,index);
				}
		}		
	}//heapSort
	
	
	public static int minNode(double [] a, int node,int index){
		double min;
		int pos;
		if((2*node + 1) < a.length - index){
			if((2*node + 2) < a.length - index){
				if(a[(2*node + 1)] > a[(2*node + 2)]){
					min = a[(2*node + 2)];
					pos = (2*node + 2);
				}
				else{
					min = a[(2*node + 1)];
					pos = (2*node + 1);

				}
				
				if(a[node] < min){
					return (-1);
				}
				else{
					return pos;
				}
				
			}// if 2nd child exists
			min = a[(2*node + 1)];
			pos = (2*node + 1);
			if(a[node] < min){
					return (-1);
				}
			else{
				return pos;
			}
		}// if 1st child exists
		return -1;	
	}//testNode
	
	public static void fixHeap(double [] a, int node, int index){
		int test = node;
		int minNode = 0;
		double holder;
		int counter = 0;
		while(true){	
			
			counter++;
			if(test >= 0){
				minNode = (minNode(a,test, index));
				if(minNode == -1){
					break;
				}
				holder = a[test];
				a[test] = a[minNode];
				a[minNode] = holder;
				test = (test-1)/2;
			}
			else{				
				break;
			}
		}		
	}//fixHeap
 
Last edited by a moderator:
Physics news on Phys.org
My suggestion for debugging this is to take a small example (say an array of 8 unsorted values) and work out exactly what you expect to be done by hand.
Then put a break point in the two places code where "holder" is set and see exactly what is happening.

On a quick look at your code, the only thing that alarmed me was the computation of treelength. Let's say that (a.length-index) is 8. What would keep treelength from being (int)2.999999999?
 
Also, I'm guessing that you enter this routine with this call:
heapSort(a, 0, 0)

I would suggest renaming heapSort to nodeSort and then creating a new heapSort with this interface:

public static void heapSort(double [] a)
 
Do you have an IDE like eclipse or netbeans? They provide an interactive debugger that can walk you thru the code line by line and you can inspect the value of your variables... It's definitely something to spend some time as it will improve your programming skill and will be useful in the future.

I use netbeans extensively at work and the debugger is just indispensable.

Have you looked at other heap sort implementations on the web? Reviewing them might elicit what is happening in your code.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K