Skip to content

Commit cc654b5

Browse files
committed
Merge branch 'gh-pages' of https://github.com/parkjs814/AlgorithmVisualizer into fix/dijkstra-visualization
2 parents 40df8b1 + 828a9f0 commit cc654b5

File tree

7 files changed

+192
-6
lines changed

7 files changed

+192
-6
lines changed

algorithm/category.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"radix": "Radix Sort",
4242
"quick": "Quicksort",
4343
"heap" : "Heapsort",
44-
"shell": "Shellsort"
44+
"shell": "Shellsort",
45+
"cycle": "Cycle Sort"
4546
}
4647
},
4748
"string": {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
logger._print( 'original array = [' + D.join(', ') + ']' );
2+
var N = D.length;
3+
var writes = 0;
4+
var pos;
5+
var item;
6+
var temp;
7+
for( cycleStart=0; cycleStart<=N-2; cycleStart++ ){
8+
item = D[cycleStart];
9+
pos = cycleStart;
10+
for( i=cycleStart+1; i<=N-1; i++ ){
11+
if( D[i]<item ){
12+
pos++;
13+
}
14+
}
15+
if( pos == cycleStart ){
16+
continue;
17+
}
18+
while( item == D[pos] ){
19+
pos++;
20+
}
21+
temp = D[pos];
22+
D[pos] = item;
23+
item = temp;
24+
25+
logger._print( 'Rewrite '+D[pos]+' to index '+pos );
26+
27+
tracer._notify(pos, D[pos])._notify(cycleStart, D[cycleStart])._wait();
28+
tracer._denotify(pos)._denotify(pos);
29+
30+
31+
while( pos != cycleStart ){
32+
pos = cycleStart;
33+
for( i=cycleStart+1; i<=N-1; i++ ){
34+
if( D[i]<item ){
35+
pos++;
36+
}
37+
}
38+
39+
while( item == D[pos] ){
40+
pos++;
41+
}
42+
temp = D[pos];
43+
D[pos] = item;
44+
item = temp;
45+
46+
logger._print( 'Rewrite '+D[pos]+' to index '+pos );
47+
48+
tracer._notify(pos, D[pos])._notify(cycleStart, D[cycleStart])._wait();
49+
tracer._denotify(pos)._denotify(pos);
50+
51+
writes++;
52+
}
53+
54+
writes++;
55+
}
56+
57+
logger._print( 'Number of writes performed is ' + writes );
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var tracer = new Array1DTracer();
2+
var logger = new LogTracer();
3+
var D = Array1D.random(15);
4+
tracer._setData(D);

algorithm/sorting/cycle/desc.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Cycle Sort": "Cycle sort is an in-place, unstable sorting algorithm, a comparison sort that is theoretically optimal in terms of the total number of writes to the original array, unlike any other in-place sorting algorithm. It is based on the idea that the permutation to be sorted can be factored into cycles, which can individually be rotated to give a sorted result.",
3+
"Complexity": {
4+
"time": "worst O(n<sup>2</sup>), best O(n<sup>2</sup>), average O(n<sup>2</sup>)",
5+
"space": "worst O(1) auxiliary"
6+
},
7+
"References": [
8+
"<a href='https://en.wikipedia.org/wiki/Cycle_sort'>Wikipedia</a>"
9+
],
10+
"files": {
11+
"basic": "Cycle Sort"
12+
}
13+
}

js/module/array1d.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ Array1DTracer.prototype = $.extend(true, Object.create(Array2DTracer.prototype),
2828
}
2929
return this;
3030
},
31+
_separate: function (idx) {
32+
this.manager.pushStep(this.capsule, {
33+
type: 'separate',
34+
x: 0,
35+
y: idx
36+
});
37+
return this;
38+
},
39+
_deseparate: function (idx) {
40+
this.manager.pushStep(this.capsule, {
41+
type: 'deseparate',
42+
x: 0,
43+
y: idx
44+
});
45+
return this;
46+
},
3147
setData: function (D) {
3248
return Array2DTracer.prototype.setData.call(this, [D]);
3349
}

js/module/array2d.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Array2DTracer.prototype = $.extend(true, Object.create(Tracer.prototype), {
130130
processStep: function (step, options) {
131131
switch (step.type) {
132132
case 'notify':
133-
if (step.v) {
133+
if (step.v === 0 || step.v) {
134134
var $row = this.$table.find('.mtbl-row').eq(step.x);
135135
var $col = $row.find('.mtbl-col').eq(step.y);
136136
$col.text(TracerUtil.refineByType(step.v));

js/script.js

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ var executeDataAndCode = function () {
158158
}
159159
$('.sidemenu button').removeClass('active');
160160
$menu.addClass('active');
161-
$('#btn_desc').click();
161+
162+
var requestedTab = getAlgorithmHash('algorithm')['tab'];
163+
if(requestedTab === 'trace') $('#btn_trace').click();
164+
else $('#btn_desc').click();
162165

163166
$('#category').html(category_name);
164167
$('#algorithm').html(algorithm_name);
@@ -249,10 +252,19 @@ var executeDataAndCode = function () {
249252
showDescription(data);
250253
showFiles(category, algorithm, files);
251254
});
255+
var hash = isScratchPaper(category, algorithm) ? algorithm : category + '/' + algorithm;
256+
setHashValue('algorithm', hash);
252257
};
253258
var list = {};
254259
var anyOpened = false;
255260
$.getJSON('./algorithm/category.json', function (data) {
261+
var algorithmHash = getAlgorithmHash();
262+
console.log(algorithmHash);
263+
var requestedCategory = algorithmHash['category'],
264+
requestedAlgorithm = algorithmHash['algorithm'];
265+
var anyRequested = requestedCategory && requestedAlgorithm;
266+
anyOpened = anyRequested;
267+
256268
list = data;
257269
for (var category in list) {
258270
(function (category) {
@@ -283,6 +295,16 @@ var executeDataAndCode = function () {
283295
}
284296
})(category);
285297
}
298+
299+
if(anyRequested) {
300+
if(!list[requestedCategory] || !list[requestedCategory].list[requestedAlgorithm]) {
301+
showErrorToast('Oops! This link appears to be broken.');
302+
$('#scratch-paper').click();
303+
} else {
304+
$('[data-category="' + requestedCategory + '"]').toggleClass('collapse');
305+
loadAlgorithm(requestedCategory, requestedAlgorithm);
306+
}
307+
}
286308
});
287309
$('#powered-by').click(function () {
288310
$('#powered-by-list button').toggleClass('collapse');
@@ -334,6 +356,7 @@ var executeDataAndCode = function () {
334356
$('#btn_share').click(function () {
335357
var $icon = $(this).find('.fa-share');
336358
$icon.addClass('fa-spin fa-spin-faster');
359+
337360
shareScratchPaper(function (url) {
338361
$icon.removeClass('fa-spin fa-spin-faster');
339362
$('#shared').removeClass('collapse');
@@ -372,12 +395,16 @@ var executeDataAndCode = function () {
372395
$('#tab_desc').addClass('active');
373396
$('.tab_bar > button').removeClass('active');
374397
$(this).addClass('active');
398+
var algorithmHash = getAlgorithmHash();
399+
setHashValue('algorithm', algorithmHash['category'] + '/' + algorithmHash['algorithm']);
375400
});
376401
$('#btn_trace').click(function () {
377402
$('.tab_container > .tab').removeClass('active');
378403
$('#tab_module').addClass('active');
379404
$('.tab_bar > button').removeClass('active');
380405
$(this).addClass('active');
406+
var algorithmHash = getAlgorithmHash();
407+
setHashValue('algorithm', algorithmHash['category'] + '/' + algorithmHash['algorithm'] + '/trace');
381408
});
382409

383410
$(window).resize(function () {
@@ -476,6 +503,74 @@ var executeDataAndCode = function () {
476503
tracerManager.findOwner(this).mousewheel(e);
477504
});
478505

506+
var getHashValue = function (key) {
507+
if(!key) return null;
508+
var hash = window.location.hash.substr(1);
509+
var params = hash ? hash.split('&') : [];
510+
for(var i = 0; i < params.length; i++) {
511+
var pair = params[i].split('=');
512+
if(pair[0] === key) {
513+
return pair[1];
514+
}
515+
}
516+
return null;
517+
}
518+
519+
var setHashValue = function (key, value) {
520+
if(!key || !value) return;
521+
var hash = window.location.hash.substr(1);
522+
var params = hash ? hash.split('&') : [];
523+
524+
var found = false;
525+
for(var i = 0; i < params.length && !found; i++) {
526+
var pair = params[i].split('=');
527+
if(pair[0] === key) {
528+
pair[1] = value;
529+
params[i] = pair.join('=');
530+
found = true;
531+
}
532+
}
533+
if(!found) {
534+
params.push([key, value].join('='));
535+
}
536+
537+
var newHash = params.join('&');
538+
window.location.hash = '#' + newHash;
539+
}
540+
541+
var removeHashValue = function (key) {
542+
if(!key) return;
543+
var hash = window.location.hash.substr(1);
544+
var params = hash ? hash.split('&') : [];
545+
546+
for(var i = 0; i < params.length; i++) {
547+
var pair = params[i].split('=');
548+
if(pair[0] === key) {
549+
params.splice(i, 1);
550+
break;
551+
}
552+
}
553+
554+
var newHash = params.join('&');
555+
window.location.hash = '#' + newHash;
556+
}
557+
558+
var getAlgorithmHash = function () {
559+
var hash = getHashValue('algorithm');
560+
if(hash){
561+
var regex = /(?:[^\/\\]+|\\.)+/g;
562+
var tmp = null, algorithmHash = {}, i = 0;
563+
while(tmp = regex.exec(hash)){
564+
if(i === 0) algorithmHash['category'] = tmp[0];
565+
if(i === 1) algorithmHash['algorithm'] = tmp[0];
566+
if(i === 2) algorithmHash['tab'] = tmp[0];
567+
i++;
568+
}
569+
return algorithmHash;
570+
} else
571+
return false;
572+
}
573+
479574
// Share scratch paper
480575

481576
var getParameterByName = function (name) {
@@ -499,7 +594,7 @@ var executeDataAndCode = function () {
499594
};
500595
$.post('https://api.github.com/gists', JSON.stringify(gist), function (res) {
501596
var data = JSON.parse(res);
502-
if (callback) callback(location.protocol + '//' + location.host + location.pathname + '?scratch-paper=' + data.id);
597+
if (callback) callback(location.protocol + '//' + location.host + location.pathname + '#scratch-paper=' + data.id);
503598
});
504599
};
505600

@@ -519,8 +614,8 @@ var executeDataAndCode = function () {
519614
});
520615
};
521616

522-
var gistID = getParameterByName('scratch-paper');
617+
var gistID = getHashValue('scratch-paper');
523618
if (gistID) {
524619
loadScratchPaper(gistID);
525620
}
526-
})();
621+
})();

0 commit comments

Comments
 (0)