Skip to content

Commit 85f66a5

Browse files
committed
added insertion sort and num of operations
1 parent ac6d39d commit 85f66a5

File tree

8 files changed

+104
-146
lines changed

8 files changed

+104
-146
lines changed

.ipynb_checkpoints/SortingVisualization-checkpoint.ipynb

Lines changed: 25 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,40 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": 11,
66
"metadata": {},
77
"outputs": [],
88
"source": [
99
"import plotly.graph_objects as go\n",
10-
"import time\n",
11-
"import random"
10+
"from sortings import bubbleSort\n",
11+
"from generateData import generateData"
1212
]
1313
},
1414
{
1515
"cell_type": "markdown",
1616
"metadata": {},
1717
"source": [
18-
"#### Defining data generation function "
18+
"### Define number of elements needed"
1919
]
2020
},
2121
{
2222
"cell_type": "code",
23-
"execution_count": 2,
23+
"execution_count": 7,
2424
"metadata": {},
2525
"outputs": [],
2626
"source": [
27-
"def generateData(n = 100):\n",
28-
" y = [ random.randint(1,250) for i in range(0, n)]\n",
29-
" return y"
30-
]
31-
},
32-
{
33-
"cell_type": "markdown",
34-
"metadata": {},
35-
"source": [
36-
"#### Bubble Sort Algorithm"
37-
]
38-
},
39-
{
40-
"cell_type": "code",
41-
"execution_count": 26,
42-
"metadata": {},
43-
"outputs": [],
44-
"source": [
45-
"def bubbleSort(bar, num, colors):\n",
46-
" \n",
47-
" data = list(bar.y)\n",
48-
" \n",
49-
" for i in range(num):\n",
50-
" for j in range(num-i-1):\n",
51-
" \n",
52-
" if data[j] > data[j+1] :\n",
53-
" colors[j] = 'crimson'\n",
54-
" colors[j+1] = 'crimson'\n",
55-
" \n",
56-
" time.sleep(0.1)\n",
57-
" \n",
58-
" bar.marker.color = colors\n",
59-
" \n",
60-
" data[j], data[j+1] = data[j+1], data[j]\n",
61-
" \n",
62-
" bar.y = tuple(data)\n",
63-
" \n",
64-
" else:\n",
65-
" colors[j] = 'green'\n",
66-
" colors[j+1] = 'green'\n",
67-
" \n",
68-
" time.sleep(0.1)\n",
69-
" \n",
70-
" bar.marker.color = colors\n",
71-
" \n",
72-
" colors[j] = 'lightslategray'\n",
73-
" colors[j+1] = 'lightslategray'\n",
74-
" \n",
75-
" time.sleep(0.1)\n",
76-
" \n",
77-
" bar.marker.color = colors\n",
78-
" \n",
79-
" colors[num-i-1] = 'blue'\n",
80-
" bar.marker.color = colors"
27+
"num = 50"
8128
]
8229
},
8330
{
8431
"cell_type": "code",
85-
"execution_count": 27,
32+
"execution_count": 8,
8633
"metadata": {},
8734
"outputs": [
8835
{
8936
"data": {
9037
"application/vnd.jupyter.widget-view+json": {
91-
"model_id": "ca893413559f4ce59f11aac3deb47226",
38+
"model_id": "f9371b5c9f754630b4394ab341a69486",
9239
"version_major": 2,
9340
"version_minor": 0
9441
},
@@ -103,31 +50,41 @@
10350
}
10451
],
10552
"source": [
106-
"colors = ['lightslategray',] * 30\n",
53+
"y, colors = generateData(num)\n",
10754
"data = [go.Bar(\n",
108-
" y= generateData(30),\n",
55+
" y= y,\n",
10956
" marker_color = colors\n",
11057
" )]\n",
11158
"layout = {'yaxis': {'showgrid':False,\n",
11259
" 'showticklabels': False,\n",
11360
" 'zeroline' : False},\n",
11461
" 'xaxis' : {'showgrid' : False,\n",
11562
" 'zeroline':False,\n",
116-
" 'showticklabels':False},\n",
63+
" 'showticklabels':False,\n",
64+
" 'title':'Number of operations: 0'},\n",
11765
" 'plot_bgcolor':'rgba(0,0,0,0)'}\n",
11866
"fig = go.FigureWidget(data, layout)\n",
119-
"fig\n",
120-
"\n"
67+
"fig"
12168
]
12269
},
12370
{
12471
"cell_type": "code",
125-
"execution_count": 28,
72+
"execution_count": 9,
12673
"metadata": {},
12774
"outputs": [],
12875
"source": [
12976
"bar = fig.data[0]\n",
130-
"bubbleSort(bar, 30, colors)"
77+
"lay = fig.layout\n",
78+
"# print(fig)"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": 10,
84+
"metadata": {},
85+
"outputs": [],
86+
"source": [
87+
"bubbleSort.sort(bar, lay, num, colors)"
13188
]
13289
}
13390
],

SortingVisualization.ipynb

Lines changed: 26 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,40 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 13,
5+
"execution_count": 1,
66
"metadata": {},
77
"outputs": [],
88
"source": [
99
"import plotly.graph_objects as go\n",
10-
"import time\n",
11-
"import random\n",
12-
"from sortings import *"
10+
"from sortings import bubbleSort, insertionSort\n",
11+
"from generateData import generateData"
1312
]
1413
},
1514
{
1615
"cell_type": "markdown",
1716
"metadata": {},
1817
"source": [
19-
"#### Defining data generation function "
18+
"### Define number of elements needed"
2019
]
2120
},
2221
{
2322
"cell_type": "code",
24-
"execution_count": 6,
23+
"execution_count": 2,
2524
"metadata": {},
2625
"outputs": [],
2726
"source": [
28-
"def generateData(n = 100):\n",
29-
" y = [ random.randint(1,250) for i in range(0, n)]\n",
30-
" return y"
31-
]
32-
},
33-
{
34-
"cell_type": "markdown",
35-
"metadata": {},
36-
"source": [
37-
"#### Bubble Sort Algorithm"
27+
"num = 50"
3828
]
3929
},
4030
{
4131
"cell_type": "code",
42-
"execution_count": 7,
32+
"execution_count": 3,
4333
"metadata": {},
4434
"outputs": [
4535
{
4636
"data": {
4737
"application/vnd.jupyter.widget-view+json": {
48-
"model_id": "960be477fad34225b5a73eecacad7360",
38+
"model_id": "6032e64a1abb49d2b6968e984c768aa7",
4939
"version_major": 2,
5040
"version_minor": 0
5141
},
@@ -60,82 +50,42 @@
6050
}
6151
],
6252
"source": [
63-
"colors = ['lightslategray',] * 100\n",
53+
"y, colors = generateData(num)\n",
6454
"data = [go.Bar(\n",
65-
" y= generateData(100),\n",
55+
" y= y,\n",
6656
" marker_color = colors\n",
6757
" )]\n",
6858
"layout = {'yaxis': {'showgrid':False,\n",
6959
" 'showticklabels': False,\n",
7060
" 'zeroline' : False},\n",
7161
" 'xaxis' : {'showgrid' : False,\n",
7262
" 'zeroline':False,\n",
73-
" 'showticklabels':False},\n",
63+
" 'showticklabels':False,\n",
64+
" 'title':'Number of operations: 0'},\n",
7465
" 'plot_bgcolor':'rgba(0,0,0,0)'}\n",
7566
"fig = go.FigureWidget(data, layout)\n",
7667
"fig"
7768
]
7869
},
7970
{
8071
"cell_type": "code",
81-
"execution_count": null,
72+
"execution_count": 4,
8273
"metadata": {},
83-
"outputs": [
84-
{
85-
"name": "stdout",
86-
"output_type": "stream",
87-
"text": [
88-
"Bar({\n",
89-
" 'marker': {'color': [lightslategray, lightslategray, lightslategray,\n",
90-
" lightslategray, lightslategray, lightslategray,\n",
91-
" lightslategray, lightslategray, lightslategray,\n",
92-
" lightslategray, lightslategray, lightslategray,\n",
93-
" lightslategray, lightslategray, lightslategray,\n",
94-
" lightslategray, lightslategray, lightslategray,\n",
95-
" lightslategray, lightslategray, lightslategray,\n",
96-
" lightslategray, lightslategray, lightslategray,\n",
97-
" lightslategray, lightslategray, lightslategray,\n",
98-
" lightslategray, lightslategray, lightslategray,\n",
99-
" lightslategray, lightslategray, lightslategray,\n",
100-
" lightslategray, lightslategray, lightslategray,\n",
101-
" lightslategray, lightslategray, lightslategray,\n",
102-
" lightslategray, lightslategray, lightslategray,\n",
103-
" lightslategray, lightslategray, lightslategray,\n",
104-
" lightslategray, lightslategray, lightslategray,\n",
105-
" lightslategray, lightslategray, lightslategray,\n",
106-
" lightslategray, lightslategray, lightslategray,\n",
107-
" lightslategray, lightslategray, lightslategray,\n",
108-
" lightslategray, lightslategray, lightslategray,\n",
109-
" lightslategray, lightslategray, lightslategray,\n",
110-
" lightslategray, lightslategray, lightslategray,\n",
111-
" lightslategray, lightslategray, lightslategray,\n",
112-
" lightslategray, lightslategray, lightslategray,\n",
113-
" lightslategray, lightslategray, lightslategray,\n",
114-
" lightslategray, lightslategray, lightslategray,\n",
115-
" lightslategray, lightslategray, lightslategray,\n",
116-
" lightslategray, lightslategray, lightslategray,\n",
117-
" lightslategray, lightslategray, lightslategray,\n",
118-
" lightslategray, lightslategray, lightslategray,\n",
119-
" lightslategray, lightslategray, lightslategray,\n",
120-
" lightslategray, lightslategray, lightslategray,\n",
121-
" lightslategray, lightslategray, lightslategray,\n",
122-
" lightslategray]},\n",
123-
" 'uid': '4be464f2-15a0-4f5b-9917-b595871298eb',\n",
124-
" 'y': [100, 152, 213, 42, 223, 9, 118, 132, 120, 163, 168, 84, 89, 25, 117, 88,\n",
125-
" 85, 173, 67, 82, 147, 131, 163, 45, 84, 150, 222, 75, 32, 101, 39, 120,\n",
126-
" 88, 92, 5, 25, 110, 38, 244, 58, 159, 95, 211, 162, 219, 8, 151, 245, 37,\n",
127-
" 129, 211, 147, 233, 39, 171, 175, 48, 189, 188, 90, 35, 190, 238, 55,\n",
128-
" 203, 159, 41, 146, 204, 246, 23, 240, 209, 213, 20, 166, 98, 111, 130,\n",
129-
" 227, 99, 92, 181, 73, 160, 193, 210, 182, 105, 47, 164, 65, 173, 236, 97,\n",
130-
" 30, 204, 159, 200, 79]\n",
131-
"})\n"
132-
]
133-
}
134-
],
74+
"outputs": [],
13575
"source": [
13676
"bar = fig.data[0]\n",
137-
"print(bar)\n",
138-
"bubbleSort.sort(bar, 100, colors)"
77+
"lay = fig.layout\n",
78+
"# print(fig)"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": null,
84+
"metadata": {},
85+
"outputs": [],
86+
"source": [
87+
"# bubbleSort.sort(bar, lay, num, colors)\n",
88+
"insertionSort.sort(bar, lay, num, colors)"
13989
]
14090
}
14191
],
506 Bytes
Binary file not shown.

generateData.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import random
2+
3+
def generateData(n=100):
4+
colors = ['lightslategray', ] * n
5+
y = [random.randint(1, 250) for i in range(0, n)]
6+
return y, colors
154 Bytes
Binary file not shown.
992 Bytes
Binary file not shown.

sortings/bubbleSort.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import time
22

3-
def sort(bar, num, colors):
3+
def sort(bar, layout, num, colors):
44

55
'''
66
bar : first attribute of figure data which consists of dictionary that stores all information of plot
7+
layout : layout parameter of the figure
78
num : number of elements to sort
89
colors : color array the consists of color info of all elements to sort
910
'''
1011

1112
data = list(bar.y)
12-
13+
count = 0
1314
for i in range(num):
1415
for j in range(num-i-1):
1516

@@ -31,6 +32,8 @@ def sort(bar, num, colors):
3132
colors[j] = 'lightslategray'
3233
colors[j+1] = 'lightslategray'
3334
bar.marker.color = colors
35+
count = count + 1
36+
layout.xaxis.title.text = "Number of operations: " + str(count)
3437

3538
colors[num-i-1] = 'blue'
3639
bar.marker.color = colors

sortings/insertionSort.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import time
2+
3+
4+
def sort(bar, layout, num, colors):
5+
'''
6+
bar : first attribute of figure data which consists of dictionary that stores all information of plot
7+
layout : layout parameter of the figure
8+
num : number of elements to sort
9+
colors : color array the consists of color info of all elements to sort
10+
'''
11+
12+
data = list(bar.y)
13+
count = 0
14+
15+
for i in range(1, num):
16+
17+
key = data[i]
18+
j = i-1
19+
20+
count += 1
21+
layout.xaxis.title.text = "Number of operations: " + str(count)
22+
23+
while j>=0 and key < data[j]:
24+
25+
data[j+1] = data[j]
26+
colors[j+1] = 'blue'
27+
colors[j] = 'yellow'
28+
bar.marker.color = colors
29+
bar.y = data
30+
time.sleep(0.1)
31+
32+
colors[j] = 'blue'
33+
bar.marker.color = colors
34+
j -= 1
35+
36+
count += 1
37+
layout.xaxis.title.text = "Number of operations: " + str(count)
38+
39+
data[j+1] = key
40+
bar.y = data
41+
colors[i] = 'blue'
42+
bar.marker.color = colors

0 commit comments

Comments
 (0)