Skip to content

Commit 8326d0b

Browse files
committed
added heap sort
1 parent d87ddb1 commit 8326d0b

File tree

5 files changed

+106
-16
lines changed

5 files changed

+106
-16
lines changed

.ipynb_checkpoints/SortingVisualization-checkpoint.ipynb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 11,
5+
"execution_count": 1,
66
"metadata": {},
77
"outputs": [],
88
"source": [
99
"import plotly.graph_objects as go\n",
10-
"from sortings import bubbleSort\n",
10+
"from sortings import bubbleSort, insertionSort, quickSort, mergeSort, selectionSort, heapSort\n",
1111
"from generateData import generateData"
1212
]
1313
},
@@ -20,7 +20,7 @@
2020
},
2121
{
2222
"cell_type": "code",
23-
"execution_count": 7,
23+
"execution_count": 2,
2424
"metadata": {},
2525
"outputs": [],
2626
"source": [
@@ -29,13 +29,13 @@
2929
},
3030
{
3131
"cell_type": "code",
32-
"execution_count": 8,
32+
"execution_count": 3,
3333
"metadata": {},
3434
"outputs": [
3535
{
3636
"data": {
3737
"application/vnd.jupyter.widget-view+json": {
38-
"model_id": "f9371b5c9f754630b4394ab341a69486",
38+
"model_id": "70e48aa2c9df4b82ac7405c677f7ae18",
3939
"version_major": 2,
4040
"version_minor": 0
4141
},
@@ -62,14 +62,15 @@
6262
" 'zeroline':False,\n",
6363
" 'showticklabels':False,\n",
6464
" 'title':'Number of operations: 0'},\n",
65-
" 'plot_bgcolor':'rgba(0,0,0,0)'}\n",
65+
" 'plot_bgcolor':'rgba(0,0,0,0)',\n",
66+
" 'modbar': False}\n",
6667
"fig = go.FigureWidget(data, layout)\n",
6768
"fig"
6869
]
6970
},
7071
{
7172
"cell_type": "code",
72-
"execution_count": 9,
73+
"execution_count": 4,
7374
"metadata": {},
7475
"outputs": [],
7576
"source": [
@@ -80,11 +81,16 @@
8081
},
8182
{
8283
"cell_type": "code",
83-
"execution_count": 10,
84+
"execution_count": 5,
8485
"metadata": {},
8586
"outputs": [],
8687
"source": [
87-
"bubbleSort.sort(bar, lay, num, colors)"
88+
"# bubbleSort.sort(bar, lay, num, colors)\n",
89+
"# insertionSort.sort(bar, lay, num, colors)\n",
90+
"# quickSort.sort(bar, lay, num, colors)\n",
91+
"# mergeSort.sort(bar, lay, num, colors)\n",
92+
"# selectionSort.sort(bar, lay, num, colors)\n",
93+
"heapSort.sort(bar, lay, num, colors)"
8894
]
8995
}
9096
],

SortingVisualization.ipynb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"outputs": [],
88
"source": [
99
"import plotly.graph_objects as go\n",
10-
"from sortings import bubbleSort, insertionSort, quickSort, mergeSort, selectionSort\n",
10+
"from sortings import bubbleSort, insertionSort, quickSort, mergeSort, selectionSort, heapSort\n",
1111
"from generateData import generateData"
1212
]
1313
},
@@ -24,7 +24,7 @@
2424
"metadata": {},
2525
"outputs": [],
2626
"source": [
27-
"num = 50"
27+
"num = 20"
2828
]
2929
},
3030
{
@@ -35,7 +35,7 @@
3535
{
3636
"data": {
3737
"application/vnd.jupyter.widget-view+json": {
38-
"model_id": "efc13b755490462eb56c4cd2566e34c5",
38+
"model_id": "319c330ac68e4a219c54781450d2ad96",
3939
"version_major": 2,
4040
"version_minor": 0
4141
},
@@ -57,13 +57,16 @@
5757
" )]\n",
5858
"layout = {'yaxis': {'showgrid':False,\n",
5959
" 'showticklabels': False,\n",
60-
" 'zeroline' : False},\n",
60+
" 'zeroline' : False,\n",
61+
" 'fixedrange':True},\n",
6162
" 'xaxis' : {'showgrid' : False,\n",
6263
" 'zeroline':False,\n",
6364
" 'showticklabels':False,\n",
65+
" 'fixedrange':True,\n",
6466
" 'title':'Number of operations: 0'},\n",
6567
" 'plot_bgcolor':'rgba(0,0,0,0)'}\n",
6668
"fig = go.FigureWidget(data, layout)\n",
69+
"# fig.show(config={'displayModeBar':False})\n",
6770
"fig"
6871
]
6972
},
@@ -75,20 +78,21 @@
7578
"source": [
7679
"bar = fig.data[0]\n",
7780
"lay = fig.layout\n",
78-
"# print(fig)"
81+
"# print(fig.data[0].y)"
7982
]
8083
},
8184
{
8285
"cell_type": "code",
83-
"execution_count": 5,
86+
"execution_count": 6,
8487
"metadata": {},
8588
"outputs": [],
8689
"source": [
8790
"# bubbleSort.sort(bar, lay, num, colors)\n",
8891
"# insertionSort.sort(bar, lay, num, colors)\n",
8992
"# quickSort.sort(bar, lay, num, colors)\n",
9093
"# mergeSort.sort(bar, lay, num, colors)\n",
91-
"selectionSort.sort(bar, lay, num, colors)"
94+
"# selectionSort.sort(bar, lay, num, colors)\n",
95+
"heapSort.sort(bar, lay, num, colors)"
9296
]
9397
}
9498
],
1.62 KB
Binary file not shown.
Binary file not shown.

sortings/heapSort.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import time
2+
3+
count = 0
4+
def heapify(arr, n, i, bar, colors, layout):
5+
# Find largest among root and children
6+
global count
7+
count += 1
8+
layout.xaxis.title.text = "Number of operations: " + str(count)
9+
largest = i
10+
l = 2 * i + 1
11+
r = 2 * i + 2
12+
13+
if l < n and arr[i] < arr[l]:
14+
colors[largest] = 'lightslategray'
15+
colors[l] = 'yellow'
16+
bar.marker.color = colors
17+
time.sleep(0.1)
18+
largest = l
19+
20+
if r < n and arr[largest] < arr[r]:
21+
colors[largest] = 'lightslategray'
22+
largest = r
23+
colors[r] = 'yellow'
24+
bar.marker.color = colors
25+
time.sleep(0.1)
26+
27+
# If root is not largest, swap with largest and continue heapifying
28+
if largest != i:
29+
colors[largest] = 'red'
30+
colors[i] = 'red'
31+
bar.marker.color = colors
32+
time.sleep(0.1)
33+
arr[i], arr[largest] = arr[largest], arr[i]
34+
bar.y = arr
35+
colors[largest] = 'lightslategray'
36+
colors[i] = 'lightslategray'
37+
bar.marker.color = colors
38+
time.sleep(0.1)
39+
heapify(arr, n, largest, bar, colors, layout)
40+
41+
42+
def sort(bar, layout, num, colors):
43+
'''
44+
bar : first attribute of figure data which consists of dictionary that stores all information of plot
45+
layout : layout parameter of the figure
46+
num : number of elements to sort
47+
colors : color array the consists of color info of all elements to sort
48+
'''
49+
50+
data = list(bar.y)
51+
global count
52+
# Build max heap
53+
for i in range(num, -1, -1):
54+
count += 1
55+
layout.xaxis.title.text = "Number of operations: " + str(count)
56+
heapify(data, num, i, bar, colors, layout)
57+
bar.y = data
58+
59+
for i in range(num-1, 0, -1):
60+
# swap
61+
count += 1
62+
layout.xaxis.title.text = "Number of operations: " + str(count)
63+
colors[i] = 'green'
64+
colors[0] = 'green'
65+
bar.marker.color = colors
66+
time.sleep(0.1)
67+
data[i], data[0] = data[0], data[i]
68+
bar.y = data
69+
colors[i] = 'blue'
70+
colors[0] = 'lightslategray'
71+
bar.marker.color = colors
72+
73+
#heapify root element
74+
heapify(data, i, 0, bar, colors, layout)
75+
bar.y = data
76+
77+
colors[0] = 'blue'
78+
bar.marker.color = colors
79+
count = 0
80+
# print(data)

0 commit comments

Comments
 (0)