Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2ab85c1

Browse files
Add files via upload
1 parent fde0179 commit 2ab85c1

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

‎HeapSortClass.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,37 @@ def heap(self, arr):
6363
arr[i], arr[0] = arr[0], arr[i]
6464
self.max_heap(arr, 0, i)
6565

66+
def min_heapify(self, arr, i, n):
67+
left = 2*i+1
68+
right = 2*i+2
69+
min_ = i
70+
71+
# check if left child is less than root
72+
if left<n and arr[left]<arr[i]:
73+
min_ = left
74+
75+
# check if right child is less than root and left child
76+
if right<n and arr[right]<arr[min_]:
77+
min_ = right
78+
79+
# check if root is the minimum of root, left and right children
80+
if min_ != i:
81+
arr[min_], arr[i] = arr[i], arr[min_]
82+
self.min_heapify(arr, min_, n)
83+
84+
def build_min_heap(self, arr):
85+
n = len(arr)
86+
for i in range(n//2, -1, -1):
87+
self.min_heapify(arr, i, n)
88+
89+
def extract_min_heap(self, arr, end):
90+
arr[end], arr[0] = arr[0], arr[end]
91+
self.min_heapify(arr, 0, end)
92+
return arr
93+
94+
def min_heap_sort(self, arr):
95+
self.build_min_heap(arr)
96+
for i in range(len(arr)-1, 0, -1):
97+
self.extract_min_heap(arr, i)
98+
return arr
99+

‎Min_Heap_Shell.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
2+
Type "help", "copyright", "credits" or "license()" for more information.
3+
>>>
4+
============ RESTART: D:\SatyamSinha\DS Programs\HeapSortClass.py ============
5+
>>> arr = [10, 2, 39, 59, 29]
6+
>>> heap_sort(arr)
7+
Traceback (most recent call last):
8+
File "<pyshell#1>", line 1, in <module>
9+
heap_sort(arr)
10+
NameError: name 'heap_sort' is not defined
11+
>>> min_heap_sort(arr)
12+
Traceback (most recent call last):
13+
File "<pyshell#2>", line 1, in <module>
14+
min_heap_sort(arr)
15+
NameError: name 'min_heap_sort' is not defined
16+
>>>
17+
============ RESTART: D:\SatyamSinha\DS Programs\HeapSortClass.py ============
18+
>>> arr = [10, 2, 39, 59, 29]
19+
>>> min_heap_sort(arr)
20+
Traceback (most recent call last):
21+
File "<pyshell#4>", line 1, in <module>
22+
min_heap_sort(arr)
23+
NameError: name 'min_heap_sort' is not defined
24+
>>> heap = HeapSort(arr)
25+
>>> heap.min_heap_sort(arr)
26+
Traceback (most recent call last):
27+
File "<pyshell#6>", line 1, in <module>
28+
heap.min_heap_sort(arr)
29+
File "D:\SatyamSinha\DS Programs\HeapSortClass.py", line 93, in min_heap_sort
30+
self.build_min_heap(arr)
31+
File "D:\SatyamSinha\DS Programs\HeapSortClass.py", line 86, in build_min_heap
32+
for i in range(len_arr//2, -1, -1):
33+
NameError: name 'len_arr' is not defined
34+
>>>
35+
============ RESTART: D:\SatyamSinha\DS Programs\HeapSortClass.py ============
36+
>>> arr = [10, 2, 39, 59, 29]
37+
>>> heap = HeapSort(arr)
38+
>>> heap.min_heap_sort(arr)
39+
[10, 39, 59, 29, 2]
40+
>>> heap.build_min_heap(arr)
41+
>>> arr
42+
[2, 10, 59, 29, 39]
43+
>>> arr = [50, 29, 290, 2890, 389, 277, 20]
44+
>>> heap.build_min_heap(arr)
45+
>>> arr
46+
[20, 29, 50, 2890, 389, 277, 290]
47+
>>>
48+
============ RESTART: D:\SatyamSinha\DS Programs\HeapSortClass.py ============
49+
>>> arr = [50, 29, 290, 2890, 389, 277, 20]
50+
>>> heap = HeapSort(arr)
51+
>>> heap.min_heap_sort(arr)
52+
[2890, 389, 290, 277, 50, 29, 20]
53+
>>>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /