Saltar al contenido

ejemplo de código de algoritmo de heapify max

Hola, tenemos la solución a lo que necesitas, continúa leyendo y la obtendrás a continuación.

Ejemplo 1: ordenación del montón

// Heap Sort in C
  
  #include <stdio.h>// Function to swap the the position of two elementsvoidswap(int*a,int*b)int temp =*a;*a =*b;*b = temp;voidheapify(int arr[],int n,int i)// Find largest among root, left child and right childint largest = i;int left =2* i +1;int right =2* i +2;if(left < n && arr[left]> arr[largest])
      largest = left;if(right < n && arr[right]> arr[largest])
      largest = right;// Swap and continue heapifying if root is not largestif(largest != i)swap(&arr[i],&arr[largest]);heapify(arr, n, largest);// Main function to do heap sortvoidheapSort(int arr[],int n)// Build max heapfor(int i = n /2-1; i >=0; i--)heapify(arr, n, i);// Heap sortfor(int i = n -1; i >=0; i--)swap(&arr[0],&arr[i]);// Heapify root element to get highest element at root againheapify(arr, i,0);// Print an arrayvoidprintArray(int arr[],int n)for(int i =0; i < n;++i)printf("%d ", arr[i]);printf("n");// Driver codeintmain()int arr[]=1,12,9,5,6,10;int n =sizeof(arr)/sizeof(arr[0]);heapSort(arr, n);printf("Sorted array is n");printArray(arr, n);

Ejemplo 2: ordenación del montón

// @see https://www.youtube.com/watch?v=H5kAcmGOn4Q

function heapify(list, size, index)
    let largest = index;
    let left = index *2+1;
    let right = left +1;if(left < size && list[left]> list[largest])
        largest = left;if(right < size && list[right]> list[largest])
        largest = right;if(largest !== index)[list[index], list[largest]]=[list[largest], list[index]];heapify(list, size, largest);return list;

function heapsort(list)const size = list.length;
    let index =~~(size /2-1);
    let last = size -1;while(index >=0)heapify(list, size,--index);while(last >=0)[list[0], list[last]]=[list[last], list[0]];heapify(list,--last,0);return list;heapsort([4,7,2,6,4,1,8,3]);

Ejemplo 3: heap sort heapify y max heap en árbol binario

Implementation of heap sort in C:

#include <stdio.h>intmain()int heap[10], array_size, i, j, c, root, temporary;printf("n Enter size of array to be sorted :");scanf("%d",&array_size);printf("n Enter the elements of array : ");for(i =0; i < array_size; i++)scanf("%d",&heap[i]);for(i =1; i < array_size; i++)
       c = i;do
           root =(c -1)/2;if(heap[root]< heap[c])/* to create MAX heap array */// if child is greater than parent swap them
               temporary = heap[root];// as structure is of complete binary tree
               heap[root]= heap[c];// it took logn steps to reach from root to leaf
               heap[c]= temporary;
           c = root;while(c !=0);printf("Heap array : ");for(i =0; i < array_size; i++)printf("%dt ", heap[i]);//printing the heap arrayfor(j = array_size -1; j >=0; j--)
       temporary = heap[0];
       heap[0]= heap[j];/* swap max element with rightmost leaf element */
       heap[j]= temporary;
       root =0;do
           c =2* root +1;/* left node of root element */if((heap[c]< heap[c +1])&& c < j-1)
               c++;if(heap[root]<heap[c]&& c<j)/* again rearrange to max heap array */
               temporary = heap[root];
               heap[root]= heap[c];
               heap[c]= temporary;
           root = c;while(c < j);printf("n The sorted array is : ");for(i =0; i < array_size; i++)printf("t %d", heap[i]);

Te invitamos a añadir valor a nuestro contenido informacional dando tu veteranía en las ilustraciones.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)


Tags :

Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *