Results 1 to 2 of 2

Thread: Heap Sorting

  1. #1
    kesav2005 is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default 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

    Java 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 12:14 PM. Reason: Please place your codes inside [code] tag next time!

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    On the first iteration of the for loop, i=1 and j=3. During the while loop, j is incremented. At the end of the while loop, i=j (which is now 4) and j becomes 2*i+1 (9). In the second while loop, the statement array[i]=array[j] causes the exception because j is 9. I could be wrong though.

Similar Threads

  1. JVm Heap memory settings
    By nagesh in forum Advanced Java
    Replies: 2
    Last Post: 09-17-2009, 06:47 PM
  2. Heap Sort
    By kesav2005 in forum Advanced Java
    Replies: 1
    Last Post: 11-13-2007, 12:40 PM
  3. when objects are removed from heap
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-09-2007, 05:25 PM
  4. setting JVM Heap sitze
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-09-2007, 05:21 PM
  5. Help with heap and stack
    By coco in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 03:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •