heapsort js code example

Example 1: heap sort javascript

var array_length;
/* to create MAX  array */  
function heap_root(input, i) {
    var left = 2 * i + 1;
    var right = 2 * i + 2;
    var max = i;

    if (left < array_length && input[left] > input[max]) {
        max = left;
    }

    if (right < array_length && input[right] > input[max])     {
        max = right;
    }

    if (max != i) {
        swap(input, i, max);
        heap_root(input, max);
    }
}

function swap(input, index_A, index_B) {
    var temp = input[index_A];

    input[index_A] = input[index_B];
    input[index_B] = temp;
}

function heapSort(input) {
    
    array_length = input.length;

    for (var i = Math.floor(array_length / 2); i >= 0; i -= 1)      {
        heap_root(input, i);
      }

    for (i = input.length - 1; i > 0; i--) {
        swap(input, 0, i);
        array_length--;
      
      
        heap_root(input, 0);
    }
}

var arr = [3, 0, 2, 5, -1, 4, 1];
heapSort(arr);
console.log(arr);

Example 2: heapsort

Implementation of heap sort in C++:

#include 
using namespace std;

// To heapify a subtree rooted with node i which is
// Heapify:- A process which helps regaining heap properties in tree after removal 
void heapify(int A[], int n, int i)
{
   int largest = i; // Initialize largest as root
   int left_child = 2 * i + 1; // left = 2*i + 1
   int right_child = 2 * i + 2; // right = 2*i + 2

   // If left child is larger than root
   if (left_child < n && A[left_child] > A[largest])
       largest = left_child;

   // If right child is larger than largest so far
   if (right_child < n && A[right_child] > A[largest])
       largest = right_child;

   // If largest is not root
   if (largest != i) {
       swap(A[i], A[largest]);

       // Recursively heapify the affected sub-tree
       heapify(A, n, largest);
   }
}

// main function to do heap sort
void heap_sort(int A[], int n)
{
   // Build heap (rearrange array)
   for (int i = n / 2 - 1; i >= 0; i--)
       heapify(A, n, i);

   // One by one extract an element from heap
   for (int i = n - 1; i >= 0; i--) {
       // Move current root to end
       swap(A[0], A[i]);

       // call max heapify on the reduced heap
       heapify(A, i, 0);
   }
}

/* A  function to print sorted Array */
void printArray(int A[], int n)
{
   for (int i = 0; i < n; ++i)
       cout << A[i] << " ";
   cout << "\n";
}

// Driver program
int main()
{
   int A[] = { 22, 19, 3, 25, 26, 7 }; // array to be sorted
   int n = sizeof(A) / sizeof(A[0]); // n is size of array

   heap_sort(A, n);

   cout << "Sorted array is \n";
   printArray(A, n);
}

Tags: