-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathselection_sort.html
More file actions
87 lines (82 loc) · 2.24 KB
/
selection_sort.html
File metadata and controls
87 lines (82 loc) · 2.24 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>普通方式显示选择排序过程</title>
</head>
<body>
<div style="margin:0 auto;width:900px;">
<canvas id="myCanvas" width="900" height="600">Your browser dosen't support the canvas element.</canvas>
</div>
<script type="text/javascript">
(function(window) {
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext('2d');
var height = canvas.height;
var width = canvas.width;
function getRandomNumber() {
var result = parseInt(Math.random() * height);
return result;
}
var lines = [];
ctx.clearRect(0, 0, width, height);
ctx.fillStyle = "#000000";
ctx.fillRect(0, 0, width, height);
ctx.lineWidth = 4;
ctx.strokeStyle = "#999999";
ctx.beginPath();
for (var i = 1; i <= width; i += 9) {
var tempHeight = getRandomNumber();
//console.log("i: " + i + "height: " + height + "tempHeight: " + tempHeight) ;
ctx.moveTo(i, height);
ctx.lineTo(i, height - tempHeight);
var line = {
y: height - tempHeight,
height: tempHeight
}
lines.push(line);
}
ctx.stroke();
function repaint() {
ctx.fillRect(0, 0, width, height);
ctx.beginPath();
var x = 1;
for (var k = 1; k < lines.length; k++) {
ctx.moveTo(x, height);
ctx.lineTo(x, lines[k].y);
x = x + 9;
}
ctx.stroke();
console.log("repaint");
}
var i = 0;
var j = 0;
var N = lines.length;
var min = i;
setTimeout(function() {
if (lines[min].height > lines[j].height) {
var temp = lines[j];
lines[j] = lines[min];
lines[min] = temp;
repaint();
}
j++;
if (j == N) {
i++;
min = i;
j = i + 1;
}
if (i < N - 1) {
setTimeout(arguments.callee, 30);
} else {
var text = "";
for (var l = 0; l < lines.length; l++) {
text = text + lines[l].height + " ";
}
console.log(text);
}
}, 30);
})(window);
</script>
</body>
</html>