Skip to content

Commit 0af6455

Browse files
author
duaraghav8@gmail
committed
Merge remote-tracking branch 'upstream/gh-pages' into gh-pages
2 parents 4a4bb60 + 5ca8a0a commit 0af6455

File tree

6 files changed

+114
-25
lines changed

6 files changed

+114
-25
lines changed

algorithm/category.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"radix": "Radix Sort",
4343
"quick": "Quicksort",
4444
"heap" : "Heapsort",
45-
"shell": "Shellsort"
45+
"shell": "Shellsort",
46+
"cycle": "Cycle Sort"
4647
}
4748
},
4849
"string": {

algorithm/graph_search/dijkstra/shortest_path/code.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@ function Dijkstra(start, end) {
33
var D = []; // D[i] indicates whether the i-th node is discovered or not
44
for (var i = 0; i < G.length; i++) D.push(false);
55
S[start] = 0; // Starting node is at distance 0 from itself
6+
tracerS._notify(start, S[start]);
67
var k = G.length;
78
while (k--) {
89
// Finding a node with the shortest distance from S[minIndex]
910
minDistance = MAX_VALUE;
1011
for (i = 0; i < G.length; i++) {
11-
tracerS._select(i)._wait();
1212
if (S[i] < minDistance && !D[i]) {
1313
minDistance = S[i];
1414
minIndex = i;
1515
}
16-
tracerS._deselect(i);
1716
}
1817
if (minDistance == MAX_VALUE) break; // If there is no edge from current node, jump out of loop
1918
D[minIndex] = true;
20-
tracerS._notify(minIndex, S[minIndex])._denotify(minIndex);
19+
tracerS._notify(minIndex);
2120
tracer._visit(minIndex)._wait();
2221
// For every unvisited neighbour of current node, we check
2322
// whether the path to it is shorter if going over the current node
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/script.js

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,10 @@ var executeDataAndCode = function () {
158158
}
159159
$('.sidemenu button').removeClass('active');
160160
$menu.addClass('active');
161-
var requestedTab = getHashValue('tab');
162-
if(requestedTab) {
163-
if(requestedTab === 'trace')
164-
$('#btn_trace').click();
165-
else
166-
$('#btn_desc').click();
167-
} else {
168-
$('#btn_desc').click();
169-
}
161+
162+
var requestedTab = getAlgorithmHash('algorithm')['tab'];
163+
if(requestedTab === 'trace') $('#btn_trace').click();
164+
else $('#btn_desc').click();
170165

171166
$('#category').html(category_name);
172167
$('#algorithm').html(algorithm_name);
@@ -257,12 +252,16 @@ var executeDataAndCode = function () {
257252
showDescription(data);
258253
showFiles(category, algorithm, files);
259254
});
255+
var hash = isScratchPaper(category, algorithm) ? algorithm : category + '/' + algorithm;
256+
setHashValue('algorithm', hash);
260257
};
261258
var list = {};
262259
var anyOpened = false;
263260
$.getJSON('./algorithm/category.json', function (data) {
264-
var requestedCategory = getHashValue('category'),
265-
requestedAlgorithm = getHashValue('algorithm');
261+
var algorithmHash = getAlgorithmHash();
262+
console.log(algorithmHash);
263+
var requestedCategory = algorithmHash['category'],
264+
requestedAlgorithm = algorithmHash['algorithm'];
266265
var anyRequested = requestedCategory && requestedAlgorithm;
267266
anyOpened = anyRequested;
268267

@@ -285,9 +284,6 @@ var executeDataAndCode = function () {
285284
.attr('data-algorithm', algorithm)
286285
.attr('data-category', category)
287286
.click(function () {
288-
setHashValue('category', category);
289-
setHashValue('algorithm', algorithm);
290-
setHashValue('tab', 'desc');
291287
loadAlgorithm(category, algorithm);
292288
});
293289
$('#list').append($algorithm);
@@ -304,9 +300,6 @@ var executeDataAndCode = function () {
304300
if(!list[requestedCategory] || !list[requestedCategory].list[requestedAlgorithm]) {
305301
showErrorToast('Oops! This link appears to be broken.');
306302
$('#scratch-paper').click();
307-
removeHashValue('category');
308-
removeHashValue('algorithm');
309-
removeHashValue('tab');
310303
} else {
311304
$('[data-category="' + requestedCategory + '"]').toggleClass('collapse');
312305
loadAlgorithm(requestedCategory, requestedAlgorithm);
@@ -363,6 +356,7 @@ var executeDataAndCode = function () {
363356
$('#btn_share').click(function () {
364357
var $icon = $(this).find('.fa-share');
365358
$icon.addClass('fa-spin fa-spin-faster');
359+
366360
shareScratchPaper(function (url) {
367361
$icon.removeClass('fa-spin fa-spin-faster');
368362
$('#shared').removeClass('collapse');
@@ -401,14 +395,16 @@ var executeDataAndCode = function () {
401395
$('#tab_desc').addClass('active');
402396
$('.tab_bar > button').removeClass('active');
403397
$(this).addClass('active');
404-
setHashValue('tab', 'desc');
398+
var algorithmHash = getAlgorithmHash();
399+
setHashValue('algorithm', algorithmHash['category'] + '/' + algorithmHash['algorithm']);
405400
});
406401
$('#btn_trace').click(function () {
407402
$('.tab_container > .tab').removeClass('active');
408403
$('#tab_module').addClass('active');
409404
$('.tab_bar > button').removeClass('active');
410405
$(this).addClass('active');
411-
setHashValue('tab', 'trace');
406+
var algorithmHash = getAlgorithmHash();
407+
setHashValue('algorithm', algorithmHash['category'] + '/' + algorithmHash['algorithm'] + '/trace');
412408
});
413409

414410
$(window).resize(function () {
@@ -519,6 +515,7 @@ var executeDataAndCode = function () {
519515
}
520516
return null;
521517
}
518+
522519
var setHashValue = function (key, value) {
523520
if(!key || !value) return;
524521
var hash = window.location.hash.substr(1);
@@ -540,6 +537,7 @@ var executeDataAndCode = function () {
540537
var newHash = params.join('&');
541538
window.location.hash = '#' + newHash;
542539
}
540+
543541
var removeHashValue = function (key) {
544542
if(!key) return;
545543
var hash = window.location.hash.substr(1);
@@ -556,6 +554,23 @@ var executeDataAndCode = function () {
556554
var newHash = params.join('&');
557555
window.location.hash = '#' + newHash;
558556
}
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+
559574
// Share scratch paper
560575

561576
var getParameterByName = function (name) {
@@ -579,7 +594,7 @@ var executeDataAndCode = function () {
579594
};
580595
$.post('https://api.github.com/gists', JSON.stringify(gist), function (res) {
581596
var data = JSON.parse(res);
582-
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);
583598
});
584599
};
585600

@@ -599,7 +614,7 @@ var executeDataAndCode = function () {
599614
});
600615
};
601616

602-
var gistID = getParameterByName('scratch-paper');
617+
var gistID = getHashValue('scratch-paper');
603618
if (gistID) {
604619
loadScratchPaper(gistID);
605620
}

0 commit comments

Comments
 (0)