-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.js
83 lines (71 loc) · 2.14 KB
/
test2.js
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
var dataset=[ 4, 8, 3, 2, 9, 10, 6, 2, 8, 7, 1];
var len = dataset.length;
var state=[];//排序数组中间状态
var highlightState=[];//交换元素中间状态,红色高亮
var fixState=[];//排序完毕元素中间状态,绿色高亮
state.push(dataset.slice());
highlightState.push([-1,-1]);
fixState.push([]);
var createDescArray = function(endNum,cnt){
var descArray =[];
for(var i=endNum; i>endNum-cnt; i--){
descArray.push(i);
}
return descArray;
}
for(var i=len-1; i>0; i--){
for(var j=0; j<i; j++){
state.push(dataset.slice());
highlightState.push([i,j]);
if(i<len-1){
fixState.push(createDescArray(len-1,len-1-i));
}else{
fixState.push([]);
}
if(dataset[i]<dataset[j]){
var tmp = dataset[i];
dataset[i] = dataset[j];
dataset[j] = tmp;
state.push(dataset.slice());
highlightState.push([i,j]);
if(i<len-1){
fixState.push(createDescArray(len-1,len-1-i));
}else{
fixState.push([]);
}
}
}
}
var draw = function() {
var s = state.shift() || [];
var s2 = highlightState.shift() ||[];
var s3 = fixState.shift()||[];
if (s.length) {
console.log("-----------------");
console.log(s);
console.log(s2);
console.log(s3);
console.log("-------------------");
d3.select("body").selectAll("div.bar").remove();
var selection=d3.select("body")
.selectAll("div.bar")
.data(s);
selection.enter()
.append("div")
.attr("class","bar")
.style("height",function(d){
return d*40+"px";
})
.style("background-color",function(d,i){
if(s3.indexOf(i)!=-1){
return "#11bb11";
}else if(s2.indexOf(i)!=-1){
return "#de1111";
}
});
selection.append("span")
.attr("class","digitLabel")
.text(function (d) {return d;});
}
}
setInterval(draw,1000);