You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Heaps can be used in sorting an array. In max-heaps, the maximum element will always be at the root. Heap Sort uses this property of heap to sort the array.
4
+
5
+
Consider an array `arr[]` which is to be sorted using Heap Sort.
6
+
7
+
- Initially build a max heap of elements in `arr[]`.
8
+
- The root element, `arr[1]`, will contain the maximum element of `arr[]`. After that, swap this element with the last element of `arr[]` and heapify the max heap excluding the last element which is already in its correct position and then decrease the length of the heap by one.
9
+
- Repeat step 2, until all the elements are in their correct position.
10
+
11
+
## Implementation
12
+
13
+
To heapify subtree rooted at index, i. n is the size of the heap.
14
+
15
+
```python
16
+
defheapify(data, n, i, draw_data, time_tick):
17
+
```
18
+
19
+
Initialize largest as root.
20
+
21
+
```python
22
+
largest = i
23
+
left =2*i +1
24
+
right =2*i +2
25
+
```
26
+
27
+
See if the left child of root exists and is greater than the root.
28
+
29
+
```python
30
+
if left < n and data[i] < data[left]:
31
+
largest = left
32
+
```
33
+
34
+
See if the right child of root exists and is greater than the root.
35
+
36
+
```python
37
+
if right < n and data[largest] < data[right]:
38
+
largest = right
39
+
```
40
+
41
+
Change root, if needed
42
+
43
+
```python
44
+
if largest != i:
45
+
data[i], data[largest] = data[largest], data[i]
46
+
```
47
+
48
+
Heapify the root.
49
+
50
+
```python
51
+
heapify(data, n, largest, draw_data, time_tick)
52
+
```
53
+
54
+
The main function to sort an array of a given size.
55
+
56
+
```python
57
+
defheap_sort(data, draw_data, time_tick):
58
+
n =len(data)
59
+
```
60
+
61
+
Build a maxheap. Since the last parent will be at ((n//2)-1) we can start at that location.
62
+
63
+
```python
64
+
for i inrange(n-1, -1, -1):
65
+
heapify(data, n, i, draw_data, time_tick)
66
+
```
67
+
68
+
One by one extract elements.
69
+
70
+
```python
71
+
for i inrange(n-1, 0, -1):
72
+
data[i], data[0] = data[0], data[i]
73
+
heapify(data, i, 0, draw_data, time_tick)
74
+
```
75
+
76
+
Draw the data being compared and finalized.
77
+
78
+
```python
79
+
draw_data(data, [YELLOWif x == i elseBLUEfor x inrange(n)])
80
+
time.sleep(time_tick)
81
+
draw_data(data, [BLUEfor x inrange(len(data))])
82
+
```
83
+
84
+
## Example
85
+
86
+
In the diagram below, initially, there is an unsorted array `arr[]` having 6 elements and then max-heap will be built.
87
+
88
+
<palign="center">
89
+
<img src="../images/heap_sort_1.png">
90
+
</p>
91
+
92
+
After building max-heap, the elements in the array `arr[]` will be:
93
+
94
+
<palign="center">
95
+
<img src="../images/heap_sort_2.png">
96
+
</p>
97
+
98
+
-*Step 1:* 8 is swapped with 5.
99
+
-*Step 2:* 8 is disconnected from the heap as 8 is the incorrect position now and.
100
+
-*Step 3:* Max-heap is created and 7 is swapped with 3.
101
+
-*Step 4:* 7 is disconnected from the heap.
102
+
-*Step 5:* Max heap is created and 5 is swapped with 1.
103
+
-*Step 6:* 5 is disconnected from the heap.
104
+
-*Step 7:* Max heap is created and 4 is swapped with 3.
105
+
-*Step 8:* 4 is disconnected from the heap.
106
+
-*Step 9:* Max heap is created and 3 is swapped with 1.
Copy file name to clipboardExpand all lines: README.md
+25-9
Original file line number
Diff line number
Diff line change
@@ -10,11 +10,11 @@ In this project, we will a Sorting Algorithm Visualizar using Python and Tkinter
10
10
<img src="images/file_structure.png"/>
11
11
</p>
12
12
13
-
`main.py` will be our main Python file which we will execute. `colors.py` will contain some hexadecimal values of colors that we'll need. The file `__init__.py`, will allow the algorithms folder to act as a Python package.
13
+
`main.py` will be our main Python file which we will execute. `colors.py` will contain some hexadecimal values of colors that we'll need. The file `__init__.py`, will allow the algorithms folder to act as a Python package.
14
14
15
15
## colors.py
16
16
17
-
In this file, we will store some hexadecimal values of colors as variables.
17
+
In this file, we will store some hexadecimal values of colors as variables.
18
18
19
19
```python
20
20
DARK_GRAY='#65696B'
@@ -32,7 +32,7 @@ PURPLE = '#BF01FB'
32
32
33
33
## main.py
34
34
35
-
This file will be our main executable file.
35
+
This file will be our main executable file.
36
36
37
37
### Interface
38
38
@@ -56,14 +56,15 @@ root.mainloop()
56
56
### Variables and Empty Functions
57
57
58
58
We will need random to create an array
59
+
59
60
```python
60
61
import random
61
62
```
62
63
63
64
Now, we will add some variables and functions. We will edit the functions one by one. For now, we will just `pass`.
64
65
65
-
66
66
*algo_list* is to select which algorithm we want to use to sort
67
+
67
68
```python
68
69
algorithm_name = StringVar()
69
70
algorithm_list = [
@@ -73,40 +74,47 @@ algorithm_list = [
73
74
'Insertion Sort',
74
75
'Quick Sort',
75
76
'Counting Sort',
76
-
'Radix Sort'
77
+
'Radix Sort',
78
+
'Heap Sort'
77
79
]
78
80
```
79
81
80
82
*speed_list* is for selecting sorting speed
83
+
81
84
```python
82
85
speed_name = StringVar()
83
86
speed_list = ['Fast', 'Medium', 'Slow']
84
87
```
85
88
86
-
This empty list will be filled with random values every time we generate a new array
89
+
This empty list will be filled with random values every time we generate a new array.
90
+
87
91
```python
88
92
data = []
89
93
```
90
94
91
-
This function will draw randomly generated list data[] on the canvas as vertical bars
95
+
This function will draw randomly generated list data[] on the canvas as vertical bars.
96
+
92
97
```python
93
98
defdraw_data(data, colorArray):
94
99
pass
95
100
```
96
101
97
102
This function will generate an array with random values every time we hit the generate button.
103
+
98
104
```python
99
105
defgenerate():
100
106
pass
101
107
```
102
108
103
109
This function will set the sorting speed
110
+
104
111
```python
105
112
defset_speed():
106
113
pass
107
114
```
108
115
109
116
This function will trigger a selected algorithm and start sorting
0 commit comments