2
import java.util.Arrays;

public class Test{

//main method
public static void main(String... args){
    int[] a = new int[]{16,14,10,8,7,9,3,2,4,1};

    System.out.println("unsorted array " + Arrays.toString(a));

    heap_sort(a);

    System.out.println("Sorted array " + Arrays.toString(a));
}

//returns left child index of given index
private static int left_child(int i){
return (i * 2 + 1);
}

//returns right child index of the given index
private static int right_child(int i){
    return (i * 2 + 2);
}


public static void max_heapify(int[] array , int index , int heap_size){
    int largest = index;
    int left = left_child(index);
    int right = right_child(index);

    if(left < heap_size && array[left] > array[index]){
        largest = left;
    }
    else largest = index;

    if (right < heap_size && array[right] > array[largest]){
        largest = right;
    }

    if(largest != index){
        //exchange array[index] with array[largest]
        int a = array[index];
        array[index] = array[largest];
        array[largest] = a;

        max_heapify(array,largest,heap_size);
    }
}

public static void build_max_heap(int[] array){
    int heap_size = array.length - 1;


    for (int i = ((array.length - 1) / 2) ; i >= 0 ; i--){
        max_heapify(array, i, heap_size);
    }
}

//main heap sort function 
public static void heap_sort(int[] array){

    int heap_size = array.length - 1;


    build_max_heap(array);

    for(int i = (array.length - 1) ; i > 0 ; i--){
        //exchange array[0] with array[i]
        int a = array[0];
        array[0] = array[i];
        array[i] = a;

        heap_size = heap_size - 1;
        max_heapify(array , 1 , heap_size);
    }
}


}

I have tried almost every possibility but it doesn't works fine .Could u find out where i am wrong. Output of my code is : unsorted array [16, 14, 10, 8, 7, 9, 3, 2, 4, 1] Sorted array [14, 10, 8, 7, 9, 3, 2, 4, 1, 16]

2 Answers 2

1

There are some problems: - Left and right child are modified because you loose the first element in the array. - In max_heapify function, the loops are <= - In heap_sort function you must pass 0 instead of 1 to the max_heapify function.

  //returns left child index of given index
    private static int left_child(int i)
    {
        return (i * 2);
    }

    //returns right child index of the given index
    private static int right_child(int i)
    {
        return (i * 2 + 1);
    }

    public static void max_heapify(int[] array, int index, int heap_size)
    {
        int largest = index;
        int left = left_child(index);
        int right = right_child(index);

        if (left <= heap_size && array[left] > array[index])
        {
            largest = left;
        }

        if (right <= heap_size && array[right] > array[largest])
        {
            largest = right;
        }

        if (largest != index)
        {
            //exchange array[index] with array[largest]
            int a = array[index];
            array[index] = array[largest];
            array[largest] = a;

            max_heapify(array, largest, heap_size);
        }
    }

    public static void build_max_heap(int[] array)
    {
        int heap_size = array.length - 1;


        for (int i = ((array.length - 1) / 2); i >= 0; i--)
        {
            max_heapify(array, i, heap_size);
        }
    }

    //main heap sort function 
    public static void heap_sort(int[] array)
    {

        int heap_size = array.Length - 1;


        build_max_heap(array);

        for (int i = (array.Length - 1); i > 0; i--)
        {
            //exchange array[0] with array[i]
            int a = array[0];
            array[0] = array[i];
            array[i] = a;

            heap_size = heap_size - 1;
            max_heapify(array, 0, heap_size);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that you loose items because the left and right child must be:

//returns left child index of given index
private static int left_child(int i){
return (i * 2);
}

//returns right child index of the given index
private static int right_child(int i){
    return (i * 2 + 1);
}

6 Comments

the result are the same?
Yeah ! results are same.
ok. Another point: if(left <= heap_size && array[left] > array[index]){ largest = left; } else largest = index; if (right <= heap_size && array[right] > array[largest]){ largest = right; }. You never arrive at the first and last element.
Same output u can see unsorted array [16, 14, 10, 8, 7, 9, 3, 2, 4, 1] Sorted array [14, 10, 8, 7, 9, 3, 2, 4, 1, 16]
When using a 0-based array (as in Java, C and many other languages) for a heap , it standard to use index i * 2 + 1 for left child and i * 2 + 2 for right child.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.