Thread: Heap Sorting
View Single Post
  #1 (permalink)  
Old 11-13-2007, 01:01 PM
kesav2005 kesav2005 is offline
Member
 
Join Date: Nov 2007
Posts: 2
kesav2005 is on a distinguished road
Heap Sorting
I m facing a error in this program...can any body help me in this regard.
im pasting my code below.
Its throwing a error message
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9

Code:
class Heapsort { public static void main (String args[]) { int array[]={55,40,80,65,71}; int i; int j; int tmp; for(i=array.length/2-1; i>=0; i--) { j=2*i+1; while (j<array.length) { if(j+1<array.length) if (array[j+1]>array[j]) j++; if(array[i]>=array[j]) { tmp=array[i]; array[i]=array[j]; array[j]=tmp; } i=j; j=2*i+1; } while(array.length>1) { array[i]--; tmp=array[i]; array[i]=array[j]; array[j]=tmp; if(j+1<array.length) if (array[j+1]>array[j]) j++; if(array[i]>=array[j]) { tmp=array[i]; array[i]=array[j]; array[j]=tmp; } i=j; j=2*i+1; } } System.out.print(" " + array); } }

Last edited by JavaBean : 11-13-2007 at 01:14 PM. Reason: Please place your codes inside [code] tag next time!
Reply With Quote
Sponsored Links