1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
---
title: Plots
layout: plots
group: "navigation"
---
<h1>Plots</h1>
<p><strong>Disclaimer:</strong> for info about the platform used to generate the plots below, visit the <a href="{{ site.baseurl }}/about.html">About</a> section.</p>
<div id="plots">
<p class="text-muted">Javascript-generated plot table is supposed to appear here.</p>
</div>
<script type="text/javascript">
var userName = 'egor-tensin';
var repoName = 'sorting_algorithms';
var plotsUrl = 'https://github.com/{userName}/{repoName}/raw/master/plots/'.format({
userName: userName,
repoName: repoName
});
var previewsPath = '{{ site.baseurl }}/img/previews/';
function Algorithm(codename, name, briefName,
minLength, maxLength,
repetitions, complexity) {
this.codename = codename;
this.name = name;
this.briefName = briefName;
this.minLength = minLength;
this.maxLength = maxLength;
this.repetitions = repetitions;
this.complexity = complexity;
}
Algorithm.prototype.getRepetitions = function(input) {
switch (typeof this.repetitions) {
case 'number':
return this.repetitions;
case 'object':
return this.repetitions[input];
}
}
Algorithm.prototype.getComplexity = function(input) {
switch (typeof this.complexity) {
case 'string':
return this.complexity;
case 'object':
return this.complexity[input];
}
}
Algorithm.prototype.buildPlotStem = function(input) {
return '{codename}_{repetitions}_{input}_{minLength}_{maxLength}'.format({
codename: this.codename,
input: input,
repetitions: this.getRepetitions(input),
minLength: this.minLength,
maxLength: this.maxLength
});
}
Algorithm.prototype.buildPlotFileName = function(input) {
return this.buildPlotStem(input) + '.png';
}
Algorithm.prototype.buildPreviewFileName = function(input) {
return this.buildPlotStem(input) + '.jpg';
}
Algorithm.prototype.buildPlotUrl = function(input) {
return plotsUrl + this.buildPlotFileName(input);
}
Algorithm.prototype.buildPreviewPath = function(input) {
return previewsPath + this.buildPreviewFileName(input);
}
Algorithm.prototype.buildPlotCaptionHtml = function(input) {
return '\
<div class="caption">\n\
<strong>{name}</strong><br/>\n\
<strong>Input:</strong> {input}<br/>\n\
<strong>Complexity:</strong> {complexity}<br/>\n\
</div>\n'.format({name: this.name,
input: input,
complexity: this.getComplexity(input)});
}
Algorithm.prototype.buildPlotHtml = function(input) {
return '\
<div class="col-xs-12 col-sm-6 col-md-4">\n\
<div class="thumbnail">\
<a class="thumbnail" href="{plotUrl}">\n\
<img class="img-responsive" src="{previewPath}"/>\n\
</a>\n\
{captionHtml}\n\
</div>\n\
</div>\n'.format({previewPath: this.buildPreviewPath(input),
plotUrl: this.buildPlotUrl(input),
captionHtml: this.buildPlotCaptionHtml(input)});
}
var algorithms = [
new Algorithm('bubble', 'Bubble sort', 'Bubble sort', 0, 200,
{sorted: 1000, randomized: 100, reversed: 100},
{sorted: 'O(<var>n</var>)',
randomized: 'O(<var>n</var><sup>2</sup>)',
reversed: 'O(<var>n</var><sup>2</sup>)'}),
new Algorithm('bubble_optimized', 'Optimized bubble sort',
'… optimized', 0, 200,
{sorted: 1000, randomized: 100, reversed: 100},
{sorted: 'O(<var>n</var>)',
randomized: 'O(<var>n</var><sup>2</sup>)',
reversed: 'O(<var>n</var><sup>2</sup>)'}),
new Algorithm('heap', 'Heapsort', 'Heapsort', 0, 200,
100, 'O(<var>n</var> log <var>n</var>)'),
new Algorithm('insertion', 'Insertion sort', 'Insertion sort', 0, 200,
{sorted: 1000, randomized: 100, reversed: 100},
{sorted: 'O(<var>n</var>)',
randomized: 'O(<var>n</var><sup>2</sup>)',
reversed: 'O(<var>n</var><sup>2</sup>)'}),
new Algorithm('merge', 'Merge sort', 'Merge sort', 0, 200,
100, 'O(<var>n</var> log <var>n</var>)'),
new Algorithm('quick_first', 'Quicksort (first element as pivot)',
'Quicksort (first element as pivot)', 0, 200,
100, {sorted: 'O(<var>n</var><sup>2</sup>)',
randomized: 'O(<var>n</var> log <var>n</var>)',
reversed: 'O(<var>n</var><sup>2</sup>)'}),
new Algorithm('quick_second', 'Quicksort (second element as pivot)',
'… second element…', 0, 200,
100, {sorted: 'O(<var>n</var><sup>2</sup>)',
randomized: 'O(<var>n</var> log <var>n</var>)',
reversed: 'O(<var>n</var><sup>2</sup>)'}),
new Algorithm('quick_middle', 'Quicksort (middle element as pivot)',
'… middle element…', 0, 200,
100, 'O(<var>n</var> log <var>n</var>)'),
new Algorithm('quick_last', 'Quicksort (last element as pivot)',
'… last element…', 0, 200,
100, {sorted: 'O(<var>n</var><sup>2</sup>)',
randomized: 'O(<var>n</var> log <var>n</var>)',
reversed: 'O(<var>n</var><sup>2</sup>)'}),
new Algorithm('quick_random', 'Quicksort (random element as pivot)',
'… random element…', 0, 200,
100, 'O(<var>n</var> log <var>n</var>)'),
new Algorithm('selection', 'Selection sort', 'Selection sort', 0, 200,
100, 'O(<var>n</var><sup>2</sup>)')
];
function buildPlotsAlgorithm(algorithm) {
return '\
<a id="plots_{codename}"></a>\n\
<h3>{name}</h3>\n\
<div class="row">\n\
{sorted}\
{randomized}\
{reversed}\n\
</div>\n'.format({name: algorithm.name,
codename: algorithm.codename,
sorted: algorithm.buildPlotHtml('sorted'),
randomized: algorithm.buildPlotHtml('randomized'),
reversed: algorithm.buildPlotHtml('reversed')});
}
function buildPlotsAllAlgorithms() {
var html = '';
for (var i = 0; i < algorithms.length; ++i) {
html += buildPlotsAlgorithm(algorithms[i]);
}
return html;
}
function buildComplexityTableRow(algorithm) {
return '\
<tr>\n\
<td><a href="#plots_{codename}">{briefName}</a></td>\n\
<td>{sorted}</td>\n\
<td>{randomized}</td>\n\
<td>{reversed}</td>\n\
</tr>\n'.format({codename: algorithm.codename,
briefName: algorithm.briefName,
sorted: algorithm.getComplexity('sorted'),
randomized: algorithm.getComplexity('randomized'),
reversed: algorithm.getComplexity('reversed')});
}
function buildComplexityTable() {
var html = '\
<div class="row">\n\
<div class="col-xs-12 col-sm-10 col-md-8">\n\
<table id="complexity_table" class="table table-bordered table-hover">\n\
<thead>\n\
<tr>\n\
<th class="text-center" rowspan="2">Algorithm</th>\n\
<th class="text-center" colspan="3">Complexity</th>\n\
</tr>\n\
<tr>\n\
<th class="text-center">sorted</th>\n\
<th class="text-center">randomized</th>\n\
<th class="text-center">reversed</th>\n\
</tr>\n\
</thead>\n\
<tbody>\n';
for (var i = 0; i < algorithms.length; ++i) {
html += buildComplexityTableRow(algorithms[i]);
}
html += '\
</tbody>\n\
</table>\n\
</div>\n\
</div>\n';
return html;
}
document.getElementById('plots').innerHTML = buildComplexityTable() + buildPlotsAllAlgorithms();
</script>
|