Answers for "heapsort python"

1

heapsort python

def buildHeap(lista, n):
    for i in range(n//2 - 1, -1, -1):
        heapify(lista, n, i)

def heapify(lista, n, i):
    largest = i  
    left = (2 * i) + 1    
    right = (2 * i) + 2 

    if left < n and lista[largest] < lista[left]:
        largest = left

    if right < n and lista[largest] < lista[right]:
        largest = right

    if largest != i:
        lista[i], lista[largest] = lista[largest], lista[i] 
        heapify(lista, n, largest) 

def heapSort(lista):
    n = len(lista)
    buildHeap(lista, n)
    
    for i in range(n-1, 0, -1):
        lista[i], lista[0] = lista[0], lista[i]
        heapify(lista, i, 0)
Posted by: Guest on September-04-2021
2

python code for heap using heapify

#Implementing Heap Using Heapify Method in Python 3
#MaxHeapify,MinHeapify,Ascending_Heapsort,Descending_Heapsort
class heap:
    
    def maxheapify(self,array):
        n=len(array)
        for i in range(n//2-1,-1,-1):
            self._maxheapify(array,n,i)
            
            
    def _maxheapify(self,array,n,i):
        l=2*i+1
        r=2*i+2
        if l<n and array[l]>array[i]:
            largest=l
        else:
            largest=i
        if r<n and array[r]>array[largest]:
            largest=r
        if (largest!=i):
            array[largest],array[i]=array[i],array[largest]
            self._maxheapify(array,n,largest)
            
            
    def minheapify(self,array):
        n = len(array)
        for i in range(n//2-1,-1,-1):
            self._minheapify(array,n,i)
            
            
    def _minheapify(self,array,n,i):
        l=2*i+1
        r=2*i+2
        if l<n and array[l]<array[i]:
            smallest = l
        else:
            smallest = i
        if r < n and array[r]<array[smallest]:
            smallest = r
        if (smallest != i):
            array[smallest], array[i] = array[i], array[smallest]
            self._minheapify(array, n, smallest)
            
            
    def descending_heapsort(self,array):
        n = len(array)
        for i in range(n // 2 - 1, -1, -1):
            self._minheapify(array, n, i)
        for i in range(n - 1, 0, -1):
            array[0], array[i] = array[i], array[0]
            self._minheapify(array, i, 0)


    def ascending_heapsort(self,array):
        n=len(array)
        for i in range(n//2-1,-1,-1):
            self._maxheapify(array,n,i)
        for i in range(n-1,0,-1):
            array[0],array[i]=array[i],array[0]
            self._maxheapify(array,i,0)

b=[550,4520,3,2340,12]
a=heap()

a.maxheapify(b)
print('Max Heapify -->',b)

a.minheapify(b)
print('Min Heapify -->',b)

a.ascending_heapsort(b)
print('Ascending Heap Sort -->',b)

a.descending_heapsort(b)
print('Descending Heap Sort -->',b)
Posted by: Guest on January-15-2021
0

Heap Sort Algorithm in Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 10 18:18:25 2019

@source: https://www.geeksforgeeks.org/heap-sort/

"""
# Python program for implementation of heap Sort 

# To heapify subtree rooted at index i. 
# n is size of heap 
def heapify(arr, n, i): 
	largest = i # Initialize largest as root 
	l = 2 * i + 1	 # left = 2*i + 1 
	r = 2 * i + 2	 # right = 2*i + 2 

	# See if left child of root exists and is 
	# greater than root 
	if l < n and arr[i] < arr[l]: 
		largest = l 

	# See if right child of root exists and is 
	# greater than root 
	if r < n and arr[largest] < arr[r]: 
		largest = r 

	# Change root, if needed 
	if largest != i: 
		arr[i],arr[largest] = arr[largest],arr[i] # swap 

		# Heapify the root. 
		heapify(arr, n, largest) 

# The main function to sort an array of given size 
def heapSort(arr): 
	n = len(arr) 

	# Build a maxheap. 
	for i in range(n, -1, -1): 
		heapify(arr, n, i) 

	# One by one extract elements 
	for i in range(n-1, 0, -1): 
		arr[i], arr[0] = arr[0], arr[i] # swap 
		heapify(arr, i, 0) 

heapSort(arr)
Posted by: Guest on August-22-2021
0

heap sort

// @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]);
Posted by: Guest on June-01-2020
0

heap sort heapify and max heap in binary tree

Implementation of heap sort in C:

#include <stdio.h>
int main()
{
   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("%d\t ", heap[i]);         //printing the heap array
   for (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]);
}
Posted by: Guest on June-05-2020
0

Heap in python

Heap Implementation at this link:

https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/Hashing
Posted by: Guest on March-29-2021

Python Answers by Framework

Browse Popular Code Answers by Language