Results 1 to 2 of 2

Thread: Heap Sort

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

    Default Heap Sort

    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

    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);
    }
    }

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    Please do not double post. This is one of the few ruels of this forum which you accepted while registering. I am closing this duplicate thread now. Thanks.

Similar Threads

  1. JVm Heap memory settings
    By nagesh in forum Advanced Java
    Replies: 2
    Last Post: 09-17-2009, 05:47 PM
  2. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  3. Heap Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-16-2008, 10:27 PM
  4. Heap Sorting
    By kesav2005 in forum New To Java
    Replies: 1
    Last Post: 11-13-2007, 04:04 PM
  5. Help with heap and stack
    By coco in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 02: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
  •