Skip to content

Commit 13a7881

Browse files
committed
add Selection Sort
1 parent 81d909a commit 13a7881

File tree

9 files changed

+58
-12
lines changed

9 files changed

+58
-12
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
var tracer = new Array1DTracer();
2-
var D = tracer.createRandomData();
2+
var D = tracer.createRandomData(15);
33
tracer.setData(D);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
tracer._print('original array = [' + D.join(', ') + ']');
2+
tracer._sleep(1000);
3+
tracer._pace(500);
4+
for (var i = 0; i < D.length - 1; i++) {
5+
var minJ = i;
6+
tracer._select(i);
7+
for (var j = i + 1; j < D.length; j++) {
8+
if (D[j] < D[minJ]) {
9+
tracer._select(j);
10+
minJ = j;
11+
tracer._deselect(j);
12+
}
13+
}
14+
if (minJ != i) {
15+
tracer._print('swap ' + D[i] + ' and ' + D[minJ]);
16+
var temp = D[i];
17+
D[i] = D[minJ];
18+
D[minJ] = temp;
19+
tracer._notify(i, minJ);
20+
}
21+
tracer._deselect(i);
22+
}
23+
tracer._print('sorted array = [' + D.join(', ') + ']');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var tracer = new Array1DTracer();
2+
var D = tracer.createRandomData(15);
3+
tracer.setData(D);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"def": "Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.",
3+
"apps": [],
4+
"cpx": {
5+
"time": "O(n<sup>2</sup>)",
6+
"space": "O(n)"
7+
},
8+
"refs": [
9+
"https://en.wikipedia.org/wiki/Selection_sort"
10+
],
11+
"files": {
12+
"basic": "Basic"
13+
}
14+
}

js/module/array1d.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ Array1DTracer.prototype.setData = function (D) {
1717
};
1818

1919
// Override
20-
Array1DTracer.prototype._notify = function (idx) {
21-
Array2DTracer.prototype._notify.call(this, 0, idx);
20+
Array1DTracer.prototype._notify = function (idx1, idx2) {
21+
if (idx2 === undefined) {
22+
Array2DTracer.prototype._notify.call(this, 0, idx1);
23+
} else {
24+
Array2DTracer.prototype._notify.call(this, 0, idx1, 0, idx2);
25+
}
2226
};
2327

2428
// Override

js/module/array2d.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ Array2DTracer.prototype.setData = function (D) {
5959
return false;
6060
};
6161

62-
Array2DTracer.prototype._notify = function (x, y) {
63-
this.pushStep({type: 'notifying', x: x, y: y, value: this.D[x][y]}, true);
64-
this.pushStep({type: 'notified', x: x, y: y}, false);
62+
Array2DTracer.prototype._notify = function (x1, y1, x2, y2) {
63+
var second = x2 !== undefined && y2 !== undefined;
64+
this.pushStep({type: 'notifying', x: x1, y: y1, value: this.D[x1][y1]}, !second);
65+
if (second) this.pushStep({type: 'notifying', x: x2, y: y2, value: this.D[x2][y2]}, true);
66+
this.pushStep({type: 'notified', x: x1, y: y1}, false);
67+
if (second) this.pushStep({type: 'notified', x: x2, y: y2}, false);
6568
};
6669

6770
Array2DTracer.prototype._select = function (sx, sy, ex, ey) {

js/module/graph.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ GraphTracer.prototype.createRandomData = function (N, ratio) {
3737
else G[i].push((Math.random() * (1 / ratio) | 0) == 0 ? 1 : 0);
3838
}
3939
}
40-
console.log(G);
4140
return G;
4241
};
4342

@@ -86,7 +85,7 @@ GraphTracer.prototype.setTreeData = function (G, root) {
8685
// Override
8786
GraphTracer.prototype.setData = function (G) {
8887
if (Tracer.prototype.setData.call(this, arguments)) return true;
89-
88+
9089
graph.clear();
9190
var nodes = [];
9291
var edges = [];

js/module/weighted_graph.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,12 @@ WeightedGraphTracer.prototype.createRandomData = function (N, ratio, min, max) {
3434
}
3535
}
3636
}
37-
console.log(G);
3837
return G;
3938
};
4039

4140
// Override
4241
WeightedGraphTracer.prototype.setData = function (G) {
43-
if (Tracer.prototype.setData.call(this, arguments)) return;
42+
if (Tracer.prototype.setData.call(this, arguments)) return true;
4443

4544
graph.clear();
4645
var nodes = [];
@@ -83,6 +82,8 @@ WeightedGraphTracer.prototype.setData = function (G) {
8382
ratio: 1
8483
});
8584
this.refresh();
85+
86+
return false;
8687
};
8788

8889
GraphTracer.prototype._weight = function (target, weight, delay) {
@@ -99,7 +100,6 @@ GraphTracer.prototype._leave = function (target, source, weight) {
99100

100101
//Override
101102
WeightedGraphTracer.prototype.processStep = function (step, options) {
102-
console.log(step);
103103
switch (step.type) {
104104
case 'weight':
105105
var targetNode = graph.nodes(n(step.target));

js/tracer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Tracer.prototype.createRandomData = function (arguments) {
3232

3333
Tracer.prototype.setData = function (arguments) {
3434
var data = JSON.stringify(arguments);
35-
if (lastData == data) return true;
35+
if (lastModule == this.module && lastData == data) return true;
3636
lastData = data;
3737
return false;
3838
};

0 commit comments

Comments
 (0)