Computer Science, asked by isosow, 6 months ago

Write the python programming

Enter the list of numbers[83 11 8 67 33 12] Sorted list [8 11 12 33 67 83 ]​

Answers

Answered by Anonymous
2

Input ( source code):-

def heapsort(alist):

build_max_heap(alist)

for i in range(len(alist) - 1, 0, -1):

alist[0], alist[i] = alist[i], alist[0]

max_heapify(alist, index=0, size=i)

def parent(i):

return (i - 1)//2

def left(i):

return 2*i + 1

def right(i):

return 2*i + 2

def build_max_heap(alist):

length = len(alist)

start = parent(length - 1)

while start >= 0:

max_heapify(alist, index=start, size=length)

start = start - 1

def max_heapify(alist, index, size):

l = left(index)

r = right(index)

if (l < size and alist[l] > alist[index]):

largest = l

else:

largest = index

if (r < size and alist[r] > alist[largest]):

largest = r

if (largest != index):

alist[largest], alist[index] = alist[index], alist[largest]

max_heapify(alist, largest, size)

alist = input('Enter the list of numbers: ').split()

alist = [int(x) for x in alist]

heapsort(alist)

print('Sorted list: ', end='')

print(alist)

Attachments:
Similar questions