java


Heapsort StackOverflow error


I repeatedly get a StackOverflow error at line marked with ** The stack overflows if I try to sort more than 3 numbers but works for arrays of 3 or less, therefore I do not think there is an infinite recursion.
Can someone explain to me why line 246 seems to be the source of the stack overflow?
Thanks
public static void heapSort(double [] a,int node, int index, boolean upcheck){
if(node < 0){
}
else if(node > a.length-index){
}
else if(upcheck){
if(!testHeap(a,node,(2*node)+1) || !testHeap(a,node,(2*node)+2) ){
int min = 0;
if(a[(2*node)+1] > a[(2*node)+2]){
min = (2*node)+2;
}
else{
min = (2*node)+1;
}
switchHeap(a,node,min);
****************************heapSort(a,(node-1)/2,index,true);*********************
}
}
else if(node == (a.length-index-1)/2){
if((2*node)+1 <= a.length-index){
if(testHeap(a,node,(2*node)+1)){
}
else{
heapSort(a,(node-1)/2,index,true);
}
if((2*node)+2 <= a.length-index){
if(testHeap(a,node,(2*node)+2)){
}
else{
heapSort(a,(node-1)/2,index,true);
}
}
}
switchHeap(a,0,a.length-index);
index++;
heapSort(a,0,index,false);
}
else{
if((2*node)+1 <= a.length-index){
if(testHeap(a,node,(2*node)+1)){
}
else{
heapSort(a,(node-1)/2,index,true);
}
heapSort(a,(2*node)+1,index,false);
if((2*node)+2 <= a.length-index){
if(testHeap(a,node,(2*node)+2)){
}
else{
heapSort(a,(node-1)/2,index,true);
}
heapSort(a,(2*node)+2,index,false);
}
}
}
}//heapSort - method
You are getting StackOverFlowError due to the recursive call you make in your code. i.e.
static void heapSort(double [] a,int node, int index, boolean upcheck)
calls itself recursively. You need to make use of break; in this kind of recursive method calls when the work is done
From the documentation,
public class StackOverflowError
extends VirtualMachineError
Thrown when a stack overflow occurs because an application recurses too deeply.
Please read this link to know more about the error. The accepted answer explains it all
Everytime you call
heapSort(a,(node-1)/2,index,true);
the stack will end up having its own set of variables (double[] , int node, int index...) .. this might be causing the problem if your input size is >3 (too much recursion..).
One solution could be to declare only one array which is to be sorted at instance level, not method level and pass just the indices in your functions instead of passing the array.
this might help - http://www.vogella.com/articles/JavaAlgorithmsMergesort/article.html

Related Links

Java generic interface hierarchy
How to specify column types in a google drive file?
Java Error “This method must return a result of type string” occurs without reason
Firefox Webdriver unable to launch with extension
MongoDb auditing is not working
In Inter thread processes why main thread behave badly, when child executed first?
Iterating JSONArray Best practice
Cannot resolve symbol 'RequiresApi', Android Studio
How does Java bytecode mnemonics differ from Jasmin?
Alert in Eclipse about the Java Scanner
java program with unknown loop
Does BindingResult consider locale as well?
how should i navigate from one window that uses an application window to another frame in another application window?
How to get the file name from an URI got from ACTION_GET_CONTENT Intent?
I am trying to turn on Hotspot with specific SSID on android device on a button click
Hibernate how to add a new row to a table with a foreign key

Categories

HOME
google-chrome-extension
asp.net-core
coq
reflection
minimum-spanning-tree
server
nullpointerexception
q
jsp-tags
google-docs
paw-app
webpack-2
baqend
mvvmcross
flyway4
in-app-purchase
uber-api
awesome-wm
cloudkit
metatrader4
ssl-client-authentication
spring-kafka
izpack
modx-revolution
etl
ups
vaadin7
workload-scheduler
highlight.js
ab-testing
oracle-coherence
emulator
paging
one-hot-encoding
opencover
hammerspoon
buildbot
su
internet-explorer-9
brunch
ejabberd-module
vision
file-rename
newline
google-rich-snippets
javascriptcore
pim
asset-pipeline
overriding
restlet
serve
occlusion
service-discovery
unobtrusive-validation
document.write
splice
grid.mvc
nand2tetris
netcdf4
node-sass
celery-task
rdfs
tropo
nstextview
kbuild
flutterwave
redundancy
specrun
elements
home-directory
fody
python-stackless
xna-4.0
ora-00900
thredds
citrus-pay
angular-local-storage
cctv
onactivityresult
message-driven-bean
xsockets.net
type-equivalence
browser-detection
onsubmit
boost-filesystem
invite
yetanotherforum
icanhaz.js
dbal
thunderbird-lightning
firefox-5
ctp4
anti-piracy
defensive-programming
rtml

Resources

Encrypt Message