0% found this document useful (0 votes)
8 views2 pages

Assignment 10

The document contains Python code for implementing a min-heap and max-heap, including functions to heapify and build these heaps. It also includes a function to find the minimum and maximum marks from a list of student marks. The main function prompts the user for the number of students and their marks, then displays the minimum and maximum marks.

Uploaded by

mnaaz052004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Assignment 10

The document contains Python code for implementing a min-heap and max-heap, including functions to heapify and build these heaps. It also includes a function to find the minimum and maximum marks from a list of student marks. The main function prompts the user for the number of students and their marks, then displays the minimum and maximum marks.

Uploaded by

mnaaz052004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Assignment 10

def heapify_min(arr, n, i):

Smallest = i

L, r = 2*i + 1, 2*i + 2

If l < n and arr[l] < arr[smallest]: smallest = l

If r < n and arr[r] < arr[smallest]: smallest = r

If smallest != i:

Arr[i], arr[smallest] = arr[smallest], arr[i]

Heapify_min(arr, n, smallest)

def build_min_heap(arr):

For i in range(len(arr)//2 – 1, -1, -1):

Heapify_min(arr, len(arr), i)

def heapify_max(arr, n, i):

Largest = i

L, r = 2*i + 1, 2*i + 2

If l < n and arr[l] > arr[largest]: largest = l

If r < n and arr[r] > arr[largest]: largest = r

If largest != i:

Arr[i], arr[largest] = arr[largest], arr[i]

Heapify_max(arr, n, largest)

def build_max_heap(arr):

For i in range(len(arr)//2 – 1, -1, -1):

Heapify_max(arr, len(arr), i)
Def find_min_max_marks(marks):

Min_heap = marks[:]

Max_heap = marks[:]

Build_min_heap(min_heap)

Build_max_heap(max_heap)

Return min_heap[0], max_heap[0]

Def main():

N = int(input(“Enter number of students: “))

Marks = [int(input(f”Enter marks of Student {i+1}: “)) for i in range(n)]

Min_marks, max_marks = find_min_max_marks(marks)

Print(“\nResults:”)

Print(“Minimum Marks:”, min_marks)

Print(“Maximum Marks:”, max_marks)

If __name__ == “__main__”:

Main()

You might also like