jmgraph 3.2.19 → 3.2.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +174 -2
  2. package/dist/jmgraph.core.min.js +1 -1
  3. package/dist/jmgraph.core.min.js.map +1 -1
  4. package/dist/jmgraph.js +1640 -135
  5. package/dist/jmgraph.min.js +1 -1
  6. package/index.js +13 -2
  7. package/package.json +1 -1
  8. package/src/core/jmGraph.js +453 -4
  9. package/src/core/jmLayer.js +142 -0
  10. package/src/core/jmPath.js +55 -0
  11. package/src/shapes/jmEllipse.js +91 -0
  12. package/src/shapes/jmLabel.js +127 -15
  13. package/src/shapes/jmPolygon.js +129 -0
  14. package/src/shapes/jmStar.js +160 -0
  15. package/example/ball.html +0 -217
  16. package/example/base.html +0 -112
  17. package/example/canvas.html +0 -54
  18. package/example/cell.html +0 -284
  19. package/example/controls/arc.html +0 -129
  20. package/example/controls/arrowline.html +0 -78
  21. package/example/controls/bezier.html +0 -299
  22. package/example/controls/img.html +0 -97
  23. package/example/controls/label.html +0 -87
  24. package/example/controls/line.html +0 -173
  25. package/example/controls/prismatic.html +0 -63
  26. package/example/controls/rect.html +0 -64
  27. package/example/controls/resize.html +0 -112
  28. package/example/controls/test.html +0 -360
  29. package/example/es.html +0 -70
  30. package/example/es5module.html +0 -63
  31. package/example/heartarc.html +0 -116
  32. package/example/index.html +0 -47
  33. package/example/js/require.js +0 -5
  34. package/example/love/img/bling/bling.tps +0 -265
  35. package/example/love/img/bling.json +0 -87
  36. package/example/love/img/bling.tps +0 -295
  37. package/example/love/img/doc/bling.gif +0 -0
  38. package/example/love/img/love.json +0 -95
  39. package/example/love/img/love.tps +0 -315
  40. package/example/love/img/qq/qq.tps +0 -399
  41. package/example/love/img/qq.json +0 -242
  42. package/example/love/index.html +0 -40
  43. package/example/love/js/game.js +0 -558
  44. package/example/music.html +0 -211
  45. package/example/node/test.js +0 -138
  46. package/example/pdf.html +0 -187
  47. package/example/progress.html +0 -173
  48. package/example/pso.html +0 -148
  49. package/example/sort.html +0 -805
  50. package/example/tweenjs.html +0 -84
  51. package/example/webgl.html +0 -278
  52. package/example/xfj/img/dr_die.gif +0 -0
  53. package/example/xfj/index.html +0 -332
  54. package/example/xfj/shake.js +0 -49
  55. package/example/xfj/testori.html +0 -76
package/example/sort.html DELETED
@@ -1,805 +0,0 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta content="text/html; charset=UTF-8" http-equiv="content-type" />
5
- <title>排序算法演示</title>
6
- <meta name="viewport" content="width=device-width,initial-scale=1">
7
- </head>
8
- <button id="btn_quick">快速排序</button>
9
- <button id="btn_straightinsertion">直接插入排序</button>
10
- <button id="btn_shell">希尔排序</button>
11
- <button id="btn_simpleselection">简单选择排序</button>
12
- <button id="btn_simpleselection2">二元选择排序</button>
13
- <button id="btn_bubble">冒泡排序</button>
14
- <button id="btn_heap">堆排序</button>
15
- <body style="width:100%;">
16
- <div id="mycanvas" style="border:1px solid #ddd;width:100%;"></div>
17
- <div id="console" style="border:1px solid #ccc;background-color:#000;color:green; width: 100%;height: 300px;overflow-y:auto;padding:4px;"></div>
18
- </body>
19
- <script type="module">
20
- import jmGraph from "../index.js";
21
-
22
- Array.prototype.toRect = function(){
23
- var rects = [];
24
- for(var i=0;i<this.length;i++) {
25
- rects.push(this[i].rect || this);
26
- }
27
- return rects;
28
- }
29
-
30
- //排序管理对象
31
- function jmSort(graph) {
32
- //原始数组个数
33
- this.arrCount = 20;
34
- //原始数组
35
- this.source = [];
36
-
37
- this.graph = graph;
38
- this.rectStyle = {
39
- stroke:'rgb(173,173,209)',
40
- lineWidth:1,
41
- close:true,
42
- fill: 'rgb(8,209,54)'
43
- };
44
-
45
- this.disableStyle = {
46
- stroke:'rgba(173,173,209,0.8)',
47
- lineWidth:1,
48
- close:true,
49
- fill: 'rgba(88,196,113,0.5)'
50
- };
51
- //this.rectStyle.shadow = this.graph.createShadow(2,2,2,'rgb(39,40,34)');
52
-
53
- this.selectRectStyle = {
54
- stroke:'rgb(120,20,80)',
55
- lineWidth: 2,
56
- zIndex: 100,
57
- close:true,
58
- fill: 'rgba(255,180,5,0.7)'
59
- };
60
- //this.selectRectStyle.shadow = this.graph.createShadow(4,4,6,'rgb(39,40,34)');
61
-
62
- //基准样式
63
- this.datumRectStyle = {
64
- stroke:'rgb(224,84,68)',
65
- lineWidth:2,
66
- close:true,
67
- zIndex: 50,
68
- //lineType: 'dotted',
69
- fill: 'rgba(224,84,68,0.7)'
70
- };
71
-
72
- this.labelStyle = {
73
- stroke:'rgb(120,20,80)',
74
- textAlign: 'center',
75
- textBaseline: 'middle',
76
- font: '12px Arial',
77
- lineWidth:1
78
- };
79
- }
80
-
81
- //延迟数组循环
82
- jmSort.prototype.arrayTimeout = function(arr, fun, endfun, t, index, end) {
83
- index = index || 0;
84
- end = end || arr.length;
85
- var t = t || 200;
86
- var self = this;
87
- function intervalArr() {
88
- if(index >= end) {endfun && endfun(); return;}
89
- var r = fun(index, arr[index], function(){
90
- index ++;
91
- self.timeoutHandler = setTimeout(intervalArr, t);
92
- });
93
- if(r === false) {
94
- endfun && endfun(); return
95
- }
96
- }
97
- intervalArr();
98
- }
99
-
100
- //柱子移动动画
101
- jmSort.prototype.rectAnimate = function(rect, cb) {
102
- if(typeof index == 'function') {
103
- cb = index;
104
- index = 0;
105
- }
106
- if(!rect.length) {
107
- rect = [rect];
108
- }
109
-
110
- var tox = [];
111
- var offx = [];
112
- var pos = [];
113
- var count = rect.length;
114
- for(var i=0;i<count;i++) {
115
- pos[i] = rect[i].rect.position;
116
- //重新计算其x坐标
117
- tox[i] = this.xStep * rect[i].index + 10;
118
- offx[i] = (tox[i] - pos[i].x) / 20;
119
- }
120
-
121
- var self = this;
122
- function move() {
123
- var complete = 0;
124
- for(var i=0;i<count;i++) {
125
- pos[i].x += offx[i];
126
- if(offx[i] ==0 || (offx[i] > 0 && pos[i].x >= tox[i]) || (offx[i] < 0 && pos[i].x <= tox[i])) {
127
- pos[i].x = tox[i];
128
- complete++;
129
- }
130
- }
131
- self.graph.redraw();
132
- if(complete >= count) {
133
- cb && cb();
134
- }
135
- else {
136
- self.aniTimeoutHandler = setTimeout(move, 100);
137
- }
138
- }
139
- move();
140
- }
141
-
142
- //播放动画
143
- jmSort.prototype.play = function(frames, callback) {
144
- if(typeof frames == 'function') {
145
- callback = frames;
146
- frames = null;
147
- }
148
- frames = frames || this.frames;
149
-
150
- if(frames.length == 0) {
151
- callback && callback();
152
- return;
153
- }
154
- var f = frames.splice(0,1)[0];//取最早的一个
155
- var self = this;
156
-
157
- if(f.msg) {
158
- log(f.msg);
159
- }
160
-
161
- if(f.move && f.move.length) {
162
- //var count = 0;
163
- //for(var i=0;i<f.move.length;i++) {
164
- this.rectAnimate(f.move, function(){
165
- //count ++;
166
- //if(count >= f.move.length) {
167
- self.play(callback);
168
- //}
169
- });
170
- //}
171
- }
172
- else if(f.sels) {
173
- this.selectRect.apply(this, f.sels);
174
- this.play(callback);
175
- //setTimeout(function(){
176
- // self.play(callback);
177
- //}, 40);
178
- }
179
- else if(f.datum) {
180
- if(this.datumLine) {
181
- var start = this.datumLine.start;
182
- var end = this.datumLine.end;
183
- start.y = end.y = f.datum.rect.position.y;
184
- this.datumLine.visible = true;
185
- }
186
- this.play(callback);
187
- }
188
- else if(f.refresh) {
189
- this.refreshGraph(f.refresh);
190
- this.play(callback);
191
- }
192
- else {
193
- this.play(callback);
194
- }
195
- }
196
-
197
- //初始化排序条件,原始数组
198
- jmSort.prototype.init = function(){
199
- this.source = [];
200
- this.frames = [];
201
- var max = 100;
202
- var offy = this.graph.height - 20;
203
- this.graph.children.clear();
204
- //当前 x轴分布单位宽度
205
- this.xStep = (this.graph.width - 20) / this.arrCount;
206
-
207
- for(var i=0;i<this.arrCount;i++) {
208
- var v = {};
209
- v.value = Math.floor(Math.random() * max);
210
- v.height = v.value / max * offy;
211
- v.toString = function(){
212
- return this.value;
213
- };
214
- this.source.push(v);
215
- }
216
- //画基准线
217
- this.datumLine = this.graph.createLine({x:0,y:0},{x:this.graph.width,y:0},this.datumRectStyle);
218
- this.datumLine.visible = false;
219
- this.graph.children.add(this.datumLine);
220
- this.refreshGraph(this.source);
221
- }
222
-
223
- jmSort.prototype.reset = function() {
224
- if(this.timeoutHandler) clearTimeout(this.timeoutHandler);
225
- if(this.aniTimeoutHandler) clearTimeout(this.aniTimeoutHandler);
226
- if(this.datumLine) this.datumLine.visible = false;
227
- //this.refreshGraph();
228
- clear();
229
- this.init();
230
- }
231
-
232
- //刷新画布
233
- jmSort.prototype.refreshGraph = function(arr) {
234
- arr = arr || this.source;
235
- //this.graph.children.clear();
236
- var offy = this.graph.height - 20;
237
- for(var i=0;i<arr.length;i++) {
238
- if(arr[i].rect) {
239
- var pos = arr[i].rect.position;
240
- pos.x = this.xStep * i + 10;
241
- }
242
- else {
243
- var pos = {};
244
- pos.x = this.xStep * i + 10;
245
- pos.y = offy - arr[i].height;
246
- arr[i].rect = this.graph.createShape('rect',{position:pos,width: 10, height: arr[i].height, style: this.rectStyle});
247
- //arr[i].item = arr[i];
248
- var label = this.graph.createShape('label',{style:this.labelStyle,position:{x:0,y:arr[i].height},value:arr[i].value,width:10,height:20});
249
- arr[i].rect.children.add(label);
250
-
251
- this.graph.children.add(arr[i].rect);
252
- }
253
- //this.graph.children.add(arr[i].rect);
254
- }
255
- //this.graph.redraw();
256
- }
257
-
258
- //选中某几个值,则加亮显示
259
- jmSort.prototype.selectRect = function(datum, sels, area) {
260
- var self = this;
261
- this.graph.children.each(function(i, rect) {
262
- if(!rect.is('jmRect')) return;
263
- if(datum && (datum.indexOf(rect) > -1)) {
264
- rect.style = self.datumRectStyle;
265
- }
266
- else if(sels && (sels.indexOf(rect) > -1)) {
267
- rect.style = self.selectRectStyle;
268
- }
269
- else if(area && (area.indexOf(rect) > -1)) {
270
- rect.style = self.rectStyle;
271
- }
272
- else {
273
- rect.style = self.disableStyle;
274
- }
275
- });
276
- //this.graph.refresh();
277
- }
278
-
279
- //快速排序
280
- //取其中一个值,把小于此值的放到其左边,大于此值的放到其右边
281
- //如此递归
282
- jmSort.prototype.quickSort = function(arr, callback) {
283
- if(typeof arr == 'function') {
284
- callback = arr;
285
- arr = null;
286
- }
287
- arr = arr || this.source;
288
-
289
- this.frames.push({msg: '快速排序:取其中一个值,把小于此值的放到其左边,大于此值的放到其右边,再分别对左右二边进行同样处理,如此递归直到最后一个元素。<br />'});
290
-
291
- var self = this;
292
- function sort(source, oldleft, oldrigth) {
293
- if(source.length <= 1) return source;
294
-
295
- self.frames.push({msg:'<br />选定区域:' + source.toString()});
296
- //取一个值做为比较对象,这里直接取中间的值(任务一个都可)
297
- var mindex = Math.floor(source.length /2);
298
- var m = source[mindex];//基准值
299
- self.frames.push({datum: m,msg:'选中' + m.value + '为基准值'});
300
-
301
- //选中当前区域
302
- var area = [];
303
- for(var i=0;i<source.length;i++) {
304
- area.push(source[i].rect);
305
- }
306
- self.frames.push({sels:[[m.rect],null, area]});
307
-
308
- var left = mindex>0?source.slice(0, mindex):[];
309
- var right = mindex>0&&mindex<source.length-1?source.slice(mindex+1):[];
310
- var middle = [m];
311
-
312
- var index = oldleft?oldleft.length:0;
313
- for(var i=0;i<source.length;i++) {
314
- var s = source[i];
315
- self.frames.push({sels:[[m.rect],[s.rect], area]});
316
- var f = {move:[]};
317
- var sindex = i;
318
- if(s.value < m.value) {
319
- if(i < mindex) continue;
320
- left.push(s);
321
- var rindex = right.indexOf(s);
322
- right.splice(rindex, 1);
323
- sindex = left.length - 1;
324
- f.move.push({
325
- rect: s.rect,
326
- index: sindex + index
327
- });
328
- f.msg = s.value + '小于基准值'+m.value+',移往基准值左边';
329
- var movearr = middle.concat(right);
330
- for(var j=0;j<rindex+middle.length;j++) {
331
- f.move.push({
332
- rect: movearr[j].rect,
333
- index: sindex + index + j + 1
334
- });
335
- }
336
- }
337
- else if(s.value > m.value) {
338
- if(i > mindex) continue;
339
- var lindex = left.indexOf(s);
340
- left.splice(lindex, 1);
341
- right.unshift(s);
342
- sindex = left.length + middle.length;
343
- f.move.push({
344
- rect: s.rect,
345
- index: sindex + index
346
- });
347
- f.msg = s.value + '大于基准值'+m.value+',移往基准值右边';
348
- var movearr = left.concat(middle);
349
- for(var j=lindex;j<movearr.length;j++) {
350
- f.move.push({
351
- rect: movearr[j].rect,
352
- index: index + j
353
- });
354
- }
355
- }
356
- else if(i != mindex) {
357
- if(i < mindex) {
358
- var lindex = left.indexOf(s);
359
- left.splice(lindex, 1);
360
- middle.unshift(s);
361
- sindex = left.length;
362
- f.move.push({
363
- rect: s.rect,
364
- index: sindex + index
365
- });
366
-
367
- f.msg = s.value + '等于基准值'+m.value+',移往基准值左边位';
368
- for(var j=lindex;j<left.length;j++) {
369
- f.move.push({
370
- rect: left[j].rect,
371
- index: index + j
372
- });
373
- }
374
- }
375
- if(i > mindex) {
376
- var rindex = right.indexOf(s);
377
- right.splice(right.indexOf(s), 1);
378
- middle.push(s);
379
- sindex = left.length + middle.length - 1;
380
- f.move.push({
381
- rect: s.rect,
382
- index: sindex + index
383
- });
384
-
385
- f.msg = s.value + '等于基准值'+m.value+',移往基准值右边位';
386
- for(var j=0;j<rindex;j++) {
387
- f.move.push({
388
- rect: right[j].rect,
389
- index: sindex + index + j + 1
390
- });
391
- }
392
- }
393
- }
394
- if(f.move.length) self.frames.push(f);
395
- }
396
-
397
- return sort(left, oldleft, middle.concat(right, oldrigth||[])).concat(middle, sort(right, (oldleft||[]).concat(left, middle)));
398
- }
399
- var result = sort(arr);
400
- this.play(function(){
401
- callback && callback(result);
402
- });
403
- return result;
404
- }
405
-
406
- //直接插入排序
407
- //将一个记录插入到已排序好的有序表中,从而得到一个新记录数增1的有序表。即:先将序列的第1个记录看成是一个有序的子序列,然后从第2个记录逐个进行插入,直至整个序列有序为止。
408
- jmSort.prototype.straightInsertionSort = function(arr, dk, callback) {
409
- if(typeof arr == 'function') {
410
- callback = arr;
411
- arr= null;
412
- }
413
- if(typeof dk == 'function') {
414
- callback = dk;
415
- dk= 0;
416
- }
417
-
418
-
419
-
420
- arr = arr || this.source;
421
- dk = dk || 1;
422
- if(dk == 1) this.frames.push({msg: '直接插入排序('+dk+'):先将序列的第1个记录看成是一个有序的子序列,然后从第2个记录逐个进行插入,直至整个序列有序为止。(实际操作就是每个元素向前比较直到值等于或小于它为止)<br />'});
423
-
424
- //拷贝一份
425
- var result = arr.slice(0);
426
- var self = this;
427
-
428
- for(var i=dk;i<result.length;i++) {
429
- var k = i;
430
- var j = k - dk;
431
- while(j>=0) {
432
- var v = result[k];
433
- var pre = result[j];
434
-
435
- this.frames.push({sels: [[v.rect], [pre.rect]]});
436
-
437
- if(v.value < pre.value) {
438
- result[j] = v;
439
- result[k] = pre;
440
-
441
- v.index = j;
442
- pre.index = k;
443
-
444
- this.frames.push({move: [{rect: pre.rect,index:k}, {rect:v.rect,index:j}], msg: v.value + '小于' + pre.value + ',互换位置,继续往前比较。'});
445
-
446
- k = j;
447
- j -= dk;
448
- }
449
- else {
450
- this.frames.push({msg: v.value + '大于或等于' + pre.value + ',中止往前比较。'});
451
- break;
452
- }
453
- }
454
- }
455
-
456
- callback && callback(result);
457
- return result;
458
- }
459
-
460
- //希尔排序
461
- //先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录“基本有序”时,再对全体记录进行依次直接插入排序。
462
- jmSort.prototype.shellSort = function(arr, callback) {
463
- if(typeof arr == 'function') {
464
- callback = arr;
465
- arr = null;
466
- }
467
- this.frames.push({msg: '希尔排序:先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录“基本有序”时,再对全体记录进行依次直接插入排序。(其实就是将直接插入排序的与前一个比较改为与前第N个比较,直接全部排完。)<br />'});
468
-
469
- arr = arr || this.source;
470
- var dk = Math.floor(arr.length / 2);
471
- var result = arr;
472
- while(dk >= 1) {
473
- this.frames.push('比较跨度:' + dk);
474
- result = this.straightInsertionSort(result, dk);
475
- dk = Math.floor(dk / 2);
476
- }
477
-
478
- this.play(function(){
479
- callback && callback(result);
480
- });
481
- return result;
482
- }
483
-
484
- //简单选择排序
485
- //在要排序的一组数中,选出最小(或者最大)的一个数与第1个位置的数交换;然后在剩下的数当中再找最小(或者最大)的与第2个位置的数交换,依次类推,直到第n-1个元素(倒数第二个数)和第n个元素(最后一个数)比较为止。
486
- jmSort.prototype.simpleSelectionSort = function(arr, callback) {
487
- if(typeof arr == 'function') {
488
- callback = arr;
489
- arr = null;
490
- }
491
- arr = arr || this.source.slice(0);
492
-
493
- this.frames.push({msg: '简单选择排序:在要排序的一组数中,选出最小(或者最大)的一个数与第1个位置的数交换;然后在剩下的数当中再找最小(或者最大)的与第2个位置的数交换,依次类推,直到第n-1个元素(倒数第二个数)和第n个元素(最后一个数)比较为止。<br />'});
494
-
495
- for(var i=0;i<arr.length-1;i++) {
496
- var min = arr[i];
497
- var minindex = i;
498
-
499
- for(var j=i+1;j<arr.length;j++) {
500
- if(min.value > arr[j].value) {
501
- min = arr[j];
502
- minindex = j;
503
- }
504
- //this.frames.push({sels:[[min.rect], [arr[j].rect]]});
505
- }
506
-
507
- if(minindex != i) {
508
- this.frames.push({sels:[[min.rect], [arr[i].rect], arr.slice(i).toRect()]});
509
- this.frames.push({move: [{rect: min.rect, index: i}, {rect: arr[i].rect, index: minindex}], msg: '最小值为:' + min.value + ',跟第'+(i+1)+'个元素互换位置。'});
510
- arr[minindex] = arr[i];
511
- arr[i] = min;
512
- }
513
- }
514
-
515
- this.play(function(){
516
- callback && callback(arr);
517
- });
518
-
519
- return arr;
520
- }
521
-
522
- //二元选择排序
523
- //简单选择排序,每趟循环只能确定一个元素排序后的定位。我们可以考虑改进为每趟循环确定两个元素(当前趟最大和最小记录)的位置,从而减少排序所需的循环次数。改进后对n个数据进行排序,最多只需进行[n/2]趟循环即可
524
- jmSort.prototype.selection2Sort = function(arr, callback) {
525
- if(typeof arr == 'function') {
526
- callback = arr;
527
- arr = null;
528
- }
529
- arr = arr || this.source.slice(0);
530
-
531
- this.frames.push({msg: '二元选择排序:简单选择排序,每趟循环只能确定一个元素排序后的定位。我们可以考虑改进为每趟循环确定两个元素(当前趟最大和最小记录)的位置,从而减少排序所需的循环次数。改进后对n个数据进行排序,最多只需进行[n/2]趟循环即可<br />'});
532
-
533
- var index = -1;
534
- var self = this;
535
- var end = Math.floor(arr.length / 2);
536
- for(var i=0;i<end;i++) {
537
- //取最小值和最大值
538
- var min = arr[i];
539
- var max = arr[i];
540
- var minindex = i;
541
- var maxindex = i;
542
-
543
- for(var j=i+1;j<arr.length-i;j++) {
544
- if(min.value > arr[j].value) {
545
- min = arr[j];
546
- minindex = j;
547
- }
548
- if(max.value <= arr[j].value) {
549
- max = arr[j];
550
- maxindex = j;
551
- }
552
- }
553
-
554
- var maxpos = j - 1;
555
- this.frames.push({sels:[[min.rect, arr[i].rect], [max.rect, arr[maxpos].rect], arr.slice(i, j).toRect()]});
556
- if(minindex != i) {
557
- this.frames.push({move: [{rect: min.rect, index: i}, {rect: arr[i].rect, index: minindex}], msg: '最小值为:' + min.value + ',跟第'+(i+1)+'个元素互换位置。'});
558
- arr[minindex] = arr[i];
559
- arr[i] = min;
560
- //如果最大值是当前起始值,则它被换到最小值位置上了
561
- //需要重新改变最大值的索引为找到的最小值的索引
562
- if(maxindex == i) {
563
- maxindex = minindex;
564
- }
565
- }
566
- if(maxindex != maxpos) {
567
- this.frames.push({move: [{rect: max.rect, index: maxpos}, {rect: arr[maxpos].rect, index: maxindex}], msg: '最大值为:' + max.value + ',跟第'+j+'个元素互换位置。'});
568
- arr[maxindex] = arr[maxpos];
569
- arr[maxpos] = min;
570
- }
571
- }
572
-
573
- this.play(function(){
574
- callback && callback(arr);
575
- });
576
-
577
- return arr;
578
- }
579
-
580
- //冒泡排序
581
- //在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。
582
- jmSort.prototype.bubbleSort = function(arr, callback) {
583
- if(typeof arr == 'function') {
584
- callback = arr;
585
- arr = null;
586
- }
587
- arr = arr || this.source.slice(0);
588
-
589
- this.frames.push({msg: '冒泡排序:在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。<br />'});
590
-
591
- var i = arr.length - 1;
592
- while(i > 0) {
593
- var pos = 0;
594
- for(var j=0;j<i;j++) {
595
- this.frames.push({
596
- sels: [[arr[j].rect],[arr[j+1].rect], arr.slice(0, i + 1).toRect()]
597
- });
598
-
599
- if(arr[j].value > arr[j+1].value) {
600
- pos = j;
601
- var tmp=arr[j];arr[j]=arr[j+1];arr[j+1]=tmp;
602
- this.frames.push({
603
- move: [{
604
- rect: tmp.rect,
605
- index: j+1
606
- },{
607
- rect: arr[j].rect,
608
- index: j
609
- }],
610
- msg: tmp.value + '大于' + arr[j].value + ',互换位置。'
611
- });
612
- }
613
- }
614
- i=pos;
615
- }
616
- this.play(function(){
617
- callback && callback(arr);
618
- });
619
- }
620
-
621
- //堆排序
622
- jmSort.prototype.heapSort = function(arr, callback) {
623
- if(typeof arr == 'function') {
624
- callback = arr;
625
- arr = null;
626
- }
627
- arr = arr || this.source.slice(0);
628
-
629
- this.frames.push({msg: '堆排序:简单讲就是把一个数组看成一个完全的二叉树,每个节点带二个子节点,不断调整节点的位置保证父节点大于二个子节点,取根节点就是它的最大值(取完再进行一次调整如此递归),直到取完为止。<br />'});
630
-
631
- var result = [];
632
- var self = this;
633
- //把堆排列为大堆
634
- //堆中每个父节点的元素值都大于等于其孩子结点(如果存在),这样的堆就是一个最大堆
635
- function bigHeap(source) {
636
- if(source.length <= 1) return source;
637
- self.frames.push({msg: '调整堆,保证父节点大于子节点。<br />'});
638
-
639
- var area = [];
640
- for(var i=0;i<source.length;i++) {
641
- area.push(source[i].rect);
642
- }
643
-
644
- for(var i=source.length;i>0;i--) {
645
- var s = source[i-1];
646
- //左右子节点
647
- var left = 2 * i;
648
- var right = left + 1;
649
-
650
- var selitems = [];
651
- if(source[left-1]) {
652
- selitems.push(source[left-1].rect);
653
- }
654
- if(source[right-1]) {
655
- selitems.push(source[right-1].rect);
656
- }
657
- self.frames.push({
658
- sels: [[s.rect],selitems, area]
659
- });
660
-
661
- //如果左子节点大于当前节点,则互换
662
- if(left <= source.length && source[left-1].value > s.value) {
663
- self.frames.push({move: [{
664
- rect: source[left-1].rect,
665
- index: i-1
666
- },{
667
- rect: s.rect,
668
- index: left - 1
669
- }], msg: '左子节点'+source[left-1].value+'大于父节点' + s.value + ',互换位置'});
670
-
671
- source[i-1] = source[left-1];
672
- source[left-1] = s;
673
- s = source[i-1];
674
-
675
- self.frames.push({
676
- sels: [[s.rect],selitems, area]
677
- });
678
- }
679
- if(right <= source.length && source[right-1].value > s.value) {
680
-
681
- self.frames.push({move: [{
682
- rect: source[right-1].rect,
683
- index: i-1
684
- },{
685
- rect: s.rect,
686
- index: right - 1
687
- }], msg: '右子节点'+source[right-1].value+'大于父节点' + s.value + ',互换位置'});
688
-
689
- source[i-1] = source[right-1];
690
- source[right-1] = s;
691
-
692
- self.frames.push({
693
- sels: [[source[i-1] .rect],selitems, area]
694
- });
695
- }
696
- }
697
- return source;
698
- }
699
-
700
- //每次取出根节点,再对余下的排为大堆,如此递归
701
- function sort(source) {
702
- source = bigHeap(source);
703
- //取最大的一个,放到结果第一个中
704
- result.unshift(source.shift());
705
-
706
- var f = {move: [{
707
- rect: result[0].rect,
708
- index: source.length
709
- }],
710
- msg: '取堆的根节点(堆中的最大值):'+result[0].value+',移到数组最后'};
711
- for(var i=0;i<source.length;i++) {
712
- f.move.push({
713
- rect: source[i].rect,
714
- index: i
715
- });
716
- }
717
- self.frames.push(f);
718
- if(source.length) {
719
- sort(source);
720
- }
721
- }
722
- sort(arr);//执行堆排序
723
- this.play(function(){
724
- callback && callback(result);
725
- });
726
-
727
- return result;
728
- }
729
-
730
- //开始
731
- var sort = null;
732
-
733
- var container = document.getElementById('mycanvas');
734
- var w = Math.min(600, document.getElementById('mycanvas').offsetWidth - 10);
735
- container.style.width = w + 'px';
736
- var h = Math.min(450, window.innerHeight - 10);
737
- container.style.height = h + 'px';
738
-
739
- //初始化jmgraph
740
- var g = jmGraph.create('mycanvas', {
741
- width: w,
742
- height: h,
743
- mode: '2d'
744
- });
745
-
746
- sort = new jmSort(g);
747
-
748
- sort.init();
749
- sort.heapSort(function(ret){
750
- console.log(ret);
751
- });
752
-
753
- //实时更新画布
754
- function update() {
755
- if(g.needUpdate) g.redraw();
756
- requestAnimationFrame(update);
757
- }
758
- update();
759
-
760
- document.getElementById('btn_quick').addEventListener('click', function(){
761
- sort.reset();
762
- sort.quickSort(null,null,null,function(ret) {
763
- this.datumLine.visible = false;
764
- this.selectRect(ret);
765
- console.log(ret);
766
- });
767
- });
768
- document.getElementById('btn_straightinsertion').addEventListener('click', function(){
769
- sort.reset();
770
- sort.straightInsertionSort(function(result){
771
- sort.play();
772
- console.log(result);
773
- });
774
- });
775
- document.getElementById('btn_shell').addEventListener('click', function(){
776
- sort.reset();
777
- sort.shellSort();
778
- });
779
- document.getElementById('btn_simpleselection').addEventListener('click', function(){
780
- sort.reset();
781
- sort.simpleSelectionSort();
782
- });
783
- document.getElementById('btn_simpleselection2').addEventListener('click', function(){
784
- sort.reset();
785
- sort.selection2Sort();
786
- });
787
- document.getElementById('btn_bubble').addEventListener('click', function(){
788
- sort.reset();
789
- sort.bubbleSort();
790
- });
791
- document.getElementById('btn_heap').addEventListener('click', function(){
792
- sort.reset();
793
- sort.heapSort();
794
- });
795
-
796
- function log(msg) {
797
- var consoleDiv = document.getElementById('console');
798
- consoleDiv.innerHTML += msg + '<br />';
799
- consoleDiv.scrollTop = consoleDiv.scrollHeight;
800
- }
801
- function clear() {
802
- document.getElementById('console').innerHTML = '';
803
- }
804
- </script>
805
- </html>