main.java
/*Alexander Papachristu
     * recursive function that uses heapsort to
     * sort an inputted array
     */
Alexander Papachristu
recursive function that uses heapsort to
sort an inputted array
Alexander Papachristu recursive function that uses heapsort to sort an inputted array
public static void heapify(int arr[], int arrLength, int x)
    {
        int largest = x; 
        int left = 2 * x+1; 
        int right = 2 * x + 2; 
        if(left < arrLength && arr[left] > arr[largest])
            largest = left;
        if(right < arrLength && arr[right] > arr[largest])
            largest = right;
        if(largest != x)
        {
            int temp = arr[x];
            arr[x] = arr[largest];
            arr[largest] = temp;
            heapify(arr,arrLength, largest); 
        }
    }
public static void heapify ( int arr [ ] , int arrLength , int x ) { int largest = x ; int left = 2 * x + 1 ; int right = 2 * x + 2 ; if ( left < arrLength && arr [ left ] > arr [ largest ] ) largest = left ; if ( right < arrLength && arr [ right ] > arr [ largest ] ) largest = right ; if ( largest != x ) { int temp = arr [ x ] ; arr [ x ] = arr [ largest ] ; arr [ largest ] = temp ; heapify ( arr , arrLength , largest ) ; } }
Alexander Papachristu
 recursive function that uses heapsort to
 sort an inputted array
Alexander Papachristu recursive function that uses heapsort to sort an inputted array
//get index of the largest element //get index of the left child from tree //get index of the right child from tree //check if left child is larger than the largest //check if right child is larger than the largest //check if largest is no longer the root //change largest value to the largest of the child values //recursively call heapify function